//用户列表jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户列表</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="login.jsp">返回登录界面</a>
<table align="center"width="500">
<tr>
<th align="left">用户ID</th>
<th align="left">用户类型ID</th>
<th align="left">用户名称</th>
<th align="left">用户真实姓名</th>
</tr>
<c:forEach items="${sessionScope.list}" var="user">
<tr>
<td align="left">${user.userid }</td>
<td align="left">${user.usertypeid}</td>
<td align="left">${user.username }</td>
<td align="left">${user.userrealname }</td>
</tr>
</c:forEach>
</table>
</body>
</html>
//登录的jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登录</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<br> <br> <br> <br> <br> <br>
<h1>用户登录</h1>
<br> <br>
<form action="login.action" method="post">
用户名:<input type="text" name="username" site="12"> <br>
密码:<input type="password" name="password" site="12"> <br>
<br>
<input type="submit" value="提交">
</form>
</center>
</body>
</html>
//strut2.xml配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="login" namespace="/" extends="struts-default">
<action name="login" class="com.icss.action.DoLogin" method="execute">
<result name="login">/login.jsp</result>
<result name="success">/success.jsp</result>
<result name="failure">/failure.jsp</result>
<result name="userlist">/userlist.jsp</result>
</action>
</package>
<package name="list" namespace="/" extends="struts-default">
<action name="userlist" class="com.icss.action.ShowList" method="execute">
<result name="userlist">/userlist.jsp</result>
</action>
</package >
</struts>
//action处理
package com.icss.action;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.icss.dao.LoginDao;
import com.icss.vo.User_Vo;
import com.opensymphony.xwork2.ActionContext;
public class DoLogin {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String execute(){
LoginDao dao=new LoginDao();
List<User_Vo> list=dao.findAll();
// ActionContext ctx=ActionContext.getContext();
// Map<String, Object> session = ctx.getSession();
// session.put("list", "list");
//
HttpSession session=ServletActionContext.getRequest().getSession();
session.setAttribute("list", list);
LoginDao lo=new LoginDao();
User_Vo vo=new User_Vo();
vo=lo.find(getUsername(), getPassword());
System.out.println("得到密码"+getPassword());
if(vo==null)
return "failure";
else
return "userlist";
}
}
//数据库通用类
package com.icss.commons;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/**
* 数据库操作类
* @author samsung
*
*/
public class DBUtil {
private static String name,pwd,url;
static {
Properties p = new Properties();
try {
p.load(DBUtil.class.getClassLoader().getResourceAsStream("conn.properties"));
Class.forName(p.getProperty("driver"));
name = p.getProperty("name");
pwd = p.getProperty("pwd");
url = p.getProperty("url");
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
/**
*
* @return
*/
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection(url,name,pwd);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}
//dao访问数据库
package com.icss.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.icss.action.DoLogin;
import com.icss.commons.DBUtil;
import com.icss.vo.User_Vo;
import com.sun.corba.se.pept.transport.Connection;
public class LoginDao {
public User_Vo find(String name,String pwd)
{
java.sql.Connection conn=DBUtil.getConnection();
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
pstmt=conn.prepareStatement("select * from user_info where user_name=?and user_pwd=?");
pstmt.setString(1,name);
pstmt.setString(2, pwd);
rs=pstmt.executeQuery();
if(rs.next())
{
User_Vo vo=new User_Vo();
String sname=rs.getString("user_name");
String spassword=rs.getString("user_pwd");
vo.setUsername(sname);
vo.setUserpwd(spassword);
return vo;
}
else
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public List<User_Vo>findAll()
{
java.sql.Connection conn=DBUtil.getConnection();
PreparedStatement pstmt=null;
ResultSet rs=null;
List<User_Vo> list=new ArrayList<User_Vo>();
try {
pstmt=conn.prepareStatement("select * from user_info");
rs=pstmt.executeQuery();
while(rs.next())
{
User_Vo vo=new User_Vo();
vo.setUserid(rs.getInt(1));
// System.out.println(rs.getInt(1));
vo.setUsertypeid(rs.getInt(2));
vo.setUsername(rs.getString(3));
vo.setUserrealname(rs.getString(5));
list.add(vo);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
//vo
package com.icss.vo;
public class User_Vo {
int userid;
int usertypeid;
String username;
String userpwd;
String userrealname;
public User_Vo() {
super();
// TODO Auto-generated constructor stub
}
public User_Vo(int userid, int usertypeid, String username, String userpwd,
String userrealname) {
super();
this.userid = userid;
this.usertypeid = usertypeid;
this.username = username;
this.userpwd = userpwd;
this.userrealname = userrealname;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public int getUsertypeid() {
return usertypeid;
}
public void setUsertypeid(int usertypeid) {
this.usertypeid = usertypeid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpwd() {
return userpwd;
}
public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
}
public String getUserrealname() {
return userrealname;
}
public void setUserrealname(String userrealname) {
this.userrealname = userrealname;
}
}