jsp页面用户名密码登录验证

默默前行的蜗牛 2014-09-10 10:28:03
想实现一个登录页面,
用户名密码输入错误,提示登录失败,用户名或密码有误,并停留在登录页。登录成功,则跳转到指定的页面

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dbutil.DbUtils;

public class Show extends HttpServlet{

public void doPost(HttpServletRequest request,HttpServletResponse response){
response.setContentType("text.html;charset=GB2312");
String username = request.getParameter("user");
String password = request.getParameter("password");
if(username!=null&&password!=null){
String sql = "select password from userinfo where user = '"+username+"'";
Connection con = DbUtils.getConnection();
ResultSet rs = DbUtils.getResultSet(con, sql);
try {
if(rs.next()){
String real = rs.getString(1);
if(real.equals(password)){
RequestDispatcher dis = request.getRequestDispatcher("a.jsp");
dis.forward(request, response);
}else{
request.getSession().setAttribute("message", "登陆失败,请重新输入");
response.sendRedirect("test.jsp");
}
}else{
request.getSession().setAttribute("message", "登陆失败,请重新输入");
response.sendRedirect("test.jsp");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
public void doGet(HttpServletRequest request,HttpServletResponse response){
doPost(request, response);
}
}

jsp:

<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="show">
用户名:<input type="text" name="user"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
<%
String mess = (String)request.getSession().getAttribute("message");
if(mess!=null){
out.println(mess);
}
%>

</body>
</html>


用request.getsession()只要不关浏览器,就一直弹出错误信息
用request得不到提示信息,
应该怎么解决这个问题?
...全文
13282 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Felery 2016-04-28
  • 打赏
  • 举报
回复
DbUtils这个是怎么定义的,有啥作用?
火柴棍的坚强 2014-09-11
  • 打赏
  • 举报
回复
提示信息放在request中,request要用 请求转发,request.getRequestDispatcher("test.jsp"); 如果存在session中就用response.sendRedirect("test.jsp"); 但是提示信息不建议保存在session中,。。得到提示提醒要就要remove掉哦,不然会一直在的
sinat_19250161 2014-09-11
  • 打赏
  • 举报
回复
学到了
引用 7 楼 u014658124 的回复:
有的网页的登录信息释放到cookie中的 你关了之后session就死了 但是cookie有保存期限 有的半小时有的半天有的一天 当年清理cookie之后那就不能登陆了 放到session中的只能在session没死的时候用
吹疯鸡 2014-09-11
  • 打赏
  • 举报
回复
有的网页的登录信息释放到cookie中的 你关了之后session就死了 但是cookie有保存期限 有的半小时有的半天有的一天 当年清理cookie之后那就不能登陆了 放到session中的只能在session没死的时候用
  • 打赏
  • 举报
回复


想实现上面图中的效果,应该怎么实现?
  • 打赏
  • 举报
回复
再请教一个问题: 在登陆某个网站时,输入用户名,密码,然后关掉网页,再点击网址仍然能进去,是因为把用户名和密码存到session里面了吗?
  • 打赏
  • 举报
回复
非常感谢您的解答。 我理解的你说的放在request是不是这样? request.setAttribute("message", "登陆失败,请重新输入"); RequestDispatcher dis = request.getRequestDispatcher("test.jsp"); dis.forward(request, response); jsp获得提示: String mess = (String)request.getAttribute("message"); if(mess!=null){ out.println(mess); }
007在学算法 2014-09-11
  • 打赏
  • 举报
回复


import java.sql.*


scott_129 2014-09-10
  • 打赏
  • 举报
回复
楼上说的对,因为session的周期是一次会话,所以只要你不关浏览器,session就一直起作用。所以进入servlet时需要先清一下session。 不过你可以把提示信息放到request里面,这样就不用每次清理了,因为request是一次请求一次响应的。
姜小白- 2014-09-10
  • 打赏
  • 举报
回复
进入doPost 方法后,先将之前的session中的message置为null request.getSession().setAttribute("message", null); 避免上一次的session中的message影响本次登录 或者楼主试一下 request.getSession().invalidate(); 看能否解决楼主的问题

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧