利用session完成登录
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>My JSP 'login.jsp' starting page</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>
<form action="/CS/servlet/LoginServlet" method="post">
用户:<input type="text" name="username"><br/>
密码: <input type="password" name="password"/><br/>
<input type="submit" value="提交"/><br/>
</form>
</body>
</html>
LoginServlet.java
package com.mjrg.login;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("password");
List<User> list=DB.getAll();
for(User user:list){
out.print(list.size()+"....................................");
if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
request.getSession().setAttribute("user", user);
response.sendRedirect("/CS/login.jsp");
return;
}
}
out.print("您输入的密码或姓名有误");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
class DB{
private static List<User> list=new ArrayList<User>();
static{
list.add(new User("aa","1234"));
list.add(new User("bb","4567"));
list.add(new User("cc","9101"));
}
public static List<User> getAll(){
return list;
}
}
user.java
package com.mjrg.login;
public class User {
private String username;
private String password;
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
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;
}
}
为什么运行后LoginServlet好像没有运行,直接跳转到一个空白页面?
------解决方案--------------------
断点一下,你可能是走了public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}这个方法了。
[解决办法]
<form action="/CS/servlet/LoginServlet" method="post"> 用户:<input type="text" name="username"><br/>
密码: <input type="password" name="password"/><br/>
<input type="submit" value="提交"/><br/>
</form>
楼上正解,这样改下
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request,response);
}
[解决办法]
response.sendRedirect("/CS/login.jsp");换成request.getRequestDispatcher("*.jsp").forward(request, response)
[解决办法]
你使用的是Post方法提交的表单,但是在Servlet里面确是用的Get方法,Servlet里面的Post方法里面什么都没有写,当然不会按照你想的去跳转了,你首先要弄清楚get方法和Post方法提交表单的区别,对于你这个程序本身而言修改方法是:将doGet方法中的内容剪切到doPost中,doGet方法中只需要写{doPost(request,response);
}就OK了。