用JSP+Servlet做一个用户登录的例子(要求用数据库,只需要“用户名”,“密码”两个字段就行),登录成功之后跳转到成功页面,在成功的页面上有三个下拉框,年,月,日(1912-2011),用户输入对应的年月日后点击“提交”跳转到另外一个页面,将刚才输入的生日信息打印出来。
运用了mvc模式写的,代码如下:
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/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=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/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=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/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=UTF-8">
<title>Insert title here</title>
</head>
<body>
您选择的年份是${year }
</body>
</html>
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/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=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/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=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/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=UTF-8">
<title>Insert title here</title>
</head>
<body>
您选择的年份是${year }
</body>
</html>
温馨提示:答案为网友推荐,仅供参考