Servlet中实现登陆后成功跳转
登陆页为index.jsp,处理的servlet为Login.java,我在Login.jsp中实现对MySQL数据库的查找,成功查到后登陆成功并跳转到main.jsp中...
登陆页为index.jsp,处理的servlet为Login.java,我在Login.jsp中实现对MySQL数据库的查找,成功查到后登陆成功并跳转到main.jsp中,但是现在登陆成功了并没有跳转到main.jsp中,还停留在Login。代码如下:
//数据库连接对象
Connection conn = null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
Statement cmd = conn.createStatement();
String selectName = "select * from user where username = '"+username+"' and password= '"+password+"'";
ResultSet reader = cmd.executeQuery(selectName);
if(reader.next())
{
response.sendRedirect("mian.jsp");
}
}catch(Exception e){
e.printStackTrace();
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
try{
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
//数据库连接路径
String conStr = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=1234&characterEncoding=GB2312";
//建立连接
conn = DriverManager.getConnection(conStr);
}
catch(Exception e){
e.printStackTrace();
}
}
} 展开
//数据库连接对象
Connection conn = null;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GB2312");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
try{
String username = request.getParameter("username");
String password = request.getParameter("password");
Statement cmd = conn.createStatement();
String selectName = "select * from user where username = '"+username+"' and password= '"+password+"'";
ResultSet reader = cmd.executeQuery(selectName);
if(reader.next())
{
response.sendRedirect("mian.jsp");
}
}catch(Exception e){
e.printStackTrace();
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
try{
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver").newInstance();
//数据库连接路径
String conStr = "jdbc:mysql://127.0.0.1:3306/book?user=root&password=1234&characterEncoding=GB2312";
//建立连接
conn = DriverManager.getConnection(conStr);
}
catch(Exception e){
e.printStackTrace();
}
}
} 展开
4个回答
展开全部
response.sendRedirect("mian.jsp"); 你redirect到mian.jsp了,而不是main.jsp
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
代码太乱了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不怎么看懂
参考资料: java
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询