login.jsp关键代码
<body>
<center>
<form action="<%=request.getContextPath() %>/userServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="登录"></td>
</tr>
</table>
</form>
</center>
</body>
userServlet关键代码
public class userServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public userServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");//处理表单中文乱码
user u=new user();
String username,password;
try{
username=request.getParameter("username");
password=request.getParameter("password");
u.setUsername(username);
u.setPassword(password);
request.getSession().setAttribute("use", u);
request.getRequestDispatcher("/Success.jsp").forward(request, response);
}catch(Exception e){
e.printStackTrace();
}
}
}
Success.jsp关键代码
<body>
<table>
<tr>
<td>用户名:</td>
<td><jsp:getProperty name="use" property="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><jsp:getProperty name="use" property="password"/></td>
</tr>
</table>
</body>