servlet登錄驗證並返回錯誤信息
阿新 • • 發佈:2018-11-21
catch setattr efi dsta sub png row uri rac index.jsp(登錄界面)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% 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"> --> <style type="text/css"> </style> </head> <body> <div> <form action="loginServlet" method="post"> <table align="center" > <tr> <td>用戶名:</td> <td><input type="text" name="username">?<font color="red"> ${requestScope.namemsg}${requestScope.nameError}</font></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password">?<font color="red"> ${requestScope.pwdError}${requestScope.pwdmsg}</font></td> </tr> <tr> <td></td> <td><input type="submit" value="登錄"> ?<input type="reset" value="取消"></td> </tr> </table> </form> </div> </body> </html>
loginServlet(登錄驗證)
package com.dwx.servlet; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dwx.bean.user; import com.dwx.bean.userDao; /** * 用戶登錄驗證 */ @WebServlet("/loginServlet") public class loginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public loginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); //判斷用戶名、密碼是否為空 if(username==""||username.length()==0){ request.setAttribute("namemsg","用戶名為空!"); request.getRequestDispatcher("index.jsp").forward(request, response); } if(password==""||password.length()==0){ request.setAttribute("pwdmsg","密碼為空!"); request.getRequestDispatcher("index.jsp").forward(request, response); } //判斷用戶名、密碼是否正確 ResultSet rst=userDao.selectUser(username, password); try { if(rst.next()){ if(password.equals(rst.getString("password"))){ request.setAttribute("username",username); request.getRequestDispatcher("items.jsp").forward(request, response); }else{ request.setAttribute("pwdError","密碼不正確!"); request.getRequestDispatcher("index.jsp").forward(request, response); } }else{ request.setAttribute("nameError","用戶名不存在!"); request.getRequestDispatcher("index.jsp").forward(request, response); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
loginServlet(登錄驗證)
package com.dwx.servlet; import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dwx.bean.user; import com.dwx.bean.userDao; /** * 用戶登錄驗證 */ @WebServlet("/loginServlet") public class loginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public loginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username=request.getParameter("username"); String password=request.getParameter("password"); //判斷用戶名、密碼是否為空 if(username==""||username.length()==0){ request.setAttribute("namemsg","用戶名為空!"); request.getRequestDispatcher("index.jsp").forward(request, response); } if(password==""||password.length()==0){ request.setAttribute("pwdmsg","密碼為空!"); request.getRequestDispatcher("index.jsp").forward(request, response); } //判斷用戶名、密碼是否正確 ResultSet rst=userDao.selectUser(username, password); try { if(rst.next()){ if(password.equals(rst.getString("password"))){ request.setAttribute("username",username); request.getRequestDispatcher("items.jsp").forward(request, response); }else{ request.setAttribute("pwdError","密碼不正確!"); request.getRequestDispatcher("index.jsp").forward(request, response); } }else{ request.setAttribute("nameError","用戶名不存在!"); request.getRequestDispatcher("index.jsp").forward(request, response); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
userDao.java(查詢用戶)
package com.dwx.bean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.dwx.util.dao;
public class userDao {
public static ResultSet selectUser(String username,String password){
Connection conn=dao.getConnection();
String sql="select * from user where username=?";
ResultSet rst=null;
try{
PreparedStatement prst=conn.prepareStatement(sql);
prst.setString(1, username);
rst=prst.executeQuery();
}catch(Exception ex){
ex.printStackTrace();
}
return rst;
}
}
servlet登錄驗證並返回錯誤信息