LoginBiz.java
package com.biz
import com.entity.User
public interface LoginBiz {
boolean checkLogin(User user) throws Exception
}
LoginBizImpl.java
package com.biz
import com.dao.LoginDao
import com.dao.LoginDaoImpl
import com.entity.User
public class LoginBizImpl implements LoginBiz {
private LoginDao dao = new LoginDaoImpl()
public boolean checkLogin(User user) throws Exception {
return dao.checkLogin(user)
}
}
LoginDao.java
package com.dao
import com.entity.User
public interface LoginDao {
boolean checkLogin(User user) throws Exception
}
LoginDaoImpl.java
package com.dao
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import com.entity.User
import com.util.SQLUtil
public class LoginDaoImpl implements LoginDao {
private SQLUtil sqlUtil = new SQLUtil()
private Connection conn = null
private PreparedStatement pstmt = null
private ResultSet rs = null
//验证用户信息
public boolean checkLogin(User user) throws Exception {
conn = sqlUtil.getConn()
String sql = "select * from user where username = ? and password = ?"
pstmt = conn.prepareStatement(sql)
pstmt.setString(1, user.getUsername())
pstmt.setString(2, user.getPassword())
rs = pstmt.executeQuery()
if(rs.next()){
return true
}
return false
}
}
User.java
package com.entity
public class User {
private int id
private String username
private String password
public int getId() {
return id
}
public void setId(int id) {
this.id = id
}
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.java
package com.servlet
import java.io.IOException
import java.io.PrintWriter
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import com.biz.LoginBiz
import com.biz.LoginBizImpl
import com.entity.User
public class LoginServlet extends HttpServlet {
private LoginBiz biz = new LoginBizImpl()
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response)
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username")
String password = request.getParameter("password")
User user = new User()
user.setUsername(username)
user.setPassword(password)
boolean b = true
try {
b = biz.checkLogin(user)
} catch (Exception e) {
e.printStackTrace()
}
if(b == true){
request.getRequestDispatcher("/success.jsp").forward(request, response)
}else{
request.getRequestDispatcher("/error.jsp").forward(request, response)
}
}
}
ShowDate.java
package com.servlet
import java.io.IOException
import java.io.PrintWriter
import javax.servlet.ServletException
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
public class ShowDate extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response)
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String year = request.getParameter("year")
request.setCharacterEncoding("utf-8")
request.setAttribute("year", year)
request.getRequestDispatcher("/showDate.jsp").forward(request, response)
}
}
SQLUtil.java
package com.util
import java.sql.Connection
import java.sql.DriverManager
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.sql.SQLException
public class SQLUtil {
private static Connection conn
private static PreparedStatement pstmt
private static ResultSet rs
//创建连接
public static Connection getConn(){
String url = "jdbc:mysql://localhost:3306/test"
String user = "root"
String password = "123456"
try {
Class.forName("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection(url, user, password)
} catch (SQLException e) {
e.printStackTrace()
return null
} catch (ClassNotFoundException e) {
e.printStackTrace()
return null
}
return conn
}
//关闭连接
public static void closeConn(Connection conn,PreparedStatement pstmt,ResultSet rs){
if(rs != null){
try {
rs.close()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
if(pstmt != null){
try {
pstmt.close()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
if(conn != null){
try {
conn.close()
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ShowDate</servlet-name>
<servlet-class>com.servlet.ShowDate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ShowDate</servlet-name>
<url-pattern>/showDate</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<form action="loginServlet" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/htmlcharset=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/htmlcharset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="showDate" method="post">
<table>
<tr>
<td>年</td>
<td>
<select name="year">
<option>2002</option>
<option>2003</option>
<option>2004</option>
<option>2005</option>
<option>2006</option>
</select>
</td>
</tr>
<tr>
<td clospan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
error.jsp
<%@ page language="java" contentType="text/htmlcharset=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/htmlcharset=UTF-8">
<title>Insert title here</title>
</head>
<body>
您输入的用户名或密码错误,请重新输入!<a href='javascript:history.go(-1)'>返回上一页</a>
</body>
</html>
showDate.jsp
<%@ page language="java" contentType="text/htmlcharset=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/htmlcharset=UTF-8">
<title>Insert title here</title>
</head>
<body>
您选择的年份是${year }
</body>
</html>
例如:在用户提交用户名和密码之后,进入樱棚到loginServlet后,通过获取页面上checkbox的脊斗则值来判断用户是否需要记住密码,如果需要的话,使用:Cookie cookie = new Cookie("testkey","testvalue")cookie.setMaxAge(3600)// 有效时间,单位是秒cookie.setPath("abc")response.addCookie(cookie)如上几句来保存cookie。在登录页面的时候,需要使用:Cookie[] cookies = request.getCookies()for (int i = 0cookies != null &&i <cookies.lengthi++) { System.out.println(cookies[i].getName() + "=" + cookies[i].getValue())}来获取cookie的值判断用户曾经是否已经记住密码,如果是的话销蠢,则直接调用cookie的值来进行登录,如果不是,则正常登录你这只是贴出来前端页面,servlet有个web.xml文件配置正确吗?名称和form表单请求名一致吗?看看配置文件有没有配置后缀,伍族比如action或do,如果有,需要加在servlet请求名后面,在登录前点击F12,检查请求是否状态为族橘大200,或者后台跳转页面是兆竖否正常调整,后端加个断点跟踪一下欢迎分享,转载请注明来源:内存溢出
评论列表(0条)