问题描述
代码://///////////////////////////////////////LoginServlet.javapackage login;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class LoginServlet extends HttpServlet {public void init() throws ServletException {}public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//response.setContentType("text/html");doPost(request,response);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");//从请求中取出用户名和密码的值 String username = request.getParameter("username"); String password = request.getParameter("password"); //生成一个ArrayList对象,并把用户名和密码的值存入该对象中. ArrayList arr = new ArrayList(); arr.add(username); arr.add(password); //生成一个session对象 HttpSession session = request.getSession(true); session.removeAttribute("username"); session.setAttribute("usrename", username); //调用模型组件loginHandler,检查该用户是checkLogin否已经注册 LoginHandler login = new LoginHandler(); try {//判断下booleanmark = login.checkLogin(arr);if(mark) response.sendRedirect("main.jsp"); else response.sendRedirect("register.jsp");} catch (Exception e) {e.printStackTrace();} }public LoginServlet() {super();}public void destroy() {super.destroy(); }}///////////////////////////////////////////LoginHandler.javapackage login;import java.sql.*;import java.util.*;class LoginHandler {public LoginHandler(){}Connection conn;PreparedStatement ps;ResultSet rs;//检查是否已经注册public boolean checkLogin(ArrayList arr){//数据库中访问组件DBPoolZ中取得连接conn = DBPool.getConnection();String name = (String)arr.get(0);String password = (String)arr.get(1);try{//rs = ps.executeQuery("select * from bbs.t_userinfo where username=? and password=?");//while(rs.next())//{//if(name.equals(rs.getString("username"))&&password.equals(rs.getString("password"))){//DBPool.dbClose(conn,ps,rs);//return true;//}//else return false;//}String sql = "select * from bbs.t_userinfo where username=? and password=?";ps = conn.prepareStatement(sql);ps.setString(1,name);ps.setString(2,password);rs = ps.executeQuery();if(rs.next()){DBPool.dbClose(conn,ps,rs);return true;}else {DBPool.dbClose(conn,ps,rs);return false;}}catch(SQLException e){return false;}}}////////////////////////////////////////////////////////////////////DBPool.javapackage login;import java.io.*;import java.sql.*;import java.util.Properties;public class DBPool { private static DBPool instance = null; //取得连接 public static synchronized Connection getConnection(){ if(instance == null){ instance = new DBPool(); } return instance._getConnection(); } private DBPool(){super();} private Connection _getConnection(){ try{ String sDBDriver = null; String sConnection = null; String sUser = null; String sPassword = null; Properties p = new Properties(); InputStream is = getClass().getResourceAsStream("/db.properties"); p.load(is); sDBDriver = p.getProperty("DBDriver",sDBDriver); sConnection = p.getProperty("Connection", sConnection); sUser = p.getProperty("User",sUser); sPassword = p.getProperty("Password",sPassword); Properties pr = new Properties(); pr.put("user", sUser); pr.put("password", sPassword); pr.put("characterEncoding", "GB2312"); pr.put("useUnicode", "TRUE"); Class.forName(sDBDriver); return DriverManager.getConnection(sConnection,pr); } catch(Exception e){ System.out.println(e); return null; } } //释放资源 public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs) throws SQLException { rs.close(); ps.close(); conn.close(); }}//////////////////////////////////db.propertiesDBDriver = com.mysql.jdbc.DriverConnection = jdbc:mysql://localhost:3306/bbs/username=? and password=?User = Password =////////////////////////////login.jsp<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><%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> <form method ="post" action="loginservlet">用户名:<input type="text" name="username" size="15"><br><br>密码:<input type="password" name="password" size="15"><br><br><input type="submit" name="submit" value="登录"><br></form> </body></html>//////////////////////////////////////////////////////两外两个页面就不写了。求高手指导指导 问题补充:wenchenyangailiuyan 写道
解决方案
response.sendRedirect("main.jsp"); response.sendRedirect("register.jsp"); 你的main.jsp,register.jsp是和login.jsp放同一个文件夹里吗?不行的话,把路径写死试试:response.sendRedirect("/project_name/xxx/xxx.jsp");
解决方案二:
改成这个试试response.sendRedirect(" request.getContextPath()/main.jsp"); 能把你的错误贴出来吗
解决方案三:
response.sendRedirect(" request.getContextPath()main.jsp"); 同理试试
解决方案四:
你这个加上path路径试试,重定向好像是相当于上下文路径而言的