servlet登录例子
1人收藏此文章, 我要收藏 发表于10个月前(2012-08-02 14:00) , 已有
599次阅读 ,共
0个评论
index.jsp
01 |
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> |
03 |
String path = request.getContextPath(); |
04 |
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; |
07 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
10 |
<base href="<%=basePath%>"> |
12 |
<title>My JSP 'index.jsp' starting page</title> |
13 |
<meta http-equiv="pragma" content="no-cache"> |
14 |
<meta http-equiv="cache-control" content="no-cache"> |
15 |
<meta http-equiv="expires" content="0"> |
16 |
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> |
17 |
<meta http-equiv="description" content="This is my page"> |
24 |
<form name="form1" method="post" action="login/servlet"> |
25 |
用户名:<input type="text" name="username"><br/> |
26 |
密 码:<input type="password" name="password"><br/> |
27 |
<input type="submit" text = "提交" > |
LoginServlet.java
02 |
import java.io.IOException; |
03 |
import javax.naming.Context; |
04 |
import javax.servlet.ServletException; |
05 |
import javax.servlet.http.HttpServlet; |
06 |
import javax.servlet.http.HttpServletRequest; |
07 |
import javax.servlet.http.HttpServletResponse; |
08 |
public class LoginSerlvet extends HttpServlet { |
10 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) |
11 |
throws ServletException, IOException { |
12 |
doPost(request,response); |
15 |
protected void doPost(HttpServletRequest request, HttpServletResponse response) |
16 |
throws ServletException, IOException { |
17 |
String username = request.getParameter("username"); |
18 |
String password = request.getParameter("password"); |
19 |
System.out.println("---username------"+username); |
20 |
System.out.println("---username------"+password); |
21 |
User user = new User(); |
22 |
user.setUsername(username); |
23 |
user.setPassword(password); |
24 |
System.out.println("----------------"+user.getUsername()); |
25 |
if(username!=""&&password!=""){ |
26 |
request.setAttribute("username", username); |
27 |
request.setAttribute("password", password); |
28 |
request.getRequestDispatcher("/success.jsp").forward(request, response); |
32 |
request.getRequestDispatcher("/error.jsp").forward(request, response); |
success.jsp
01 |
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> |
02 |
<%@ page import="com.cn.*" %> |
04 |
String path = request.getContextPath(); |
05 |
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; |
08 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
11 |
<base href="<%=basePath%>"> |
13 |
<title>My JSP 'success.jsp' starting page</title> |
15 |
<meta http-equiv="pragma" content="no-cache"> |
16 |
<meta http-equiv="cache-control" content="no-cache"> |
17 |
<meta http-equiv="expires" content="0"> |
18 |
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> |
19 |
<meta http-equiv="description" content="This is my page"> |
27 |
登录成功:用户名为:<%=request.getAttribute("username")%> <br> |
web.xml
01 |
<?xml version="1.0" encoding="UTF-8"?> |
02 |
<web-app version="2.4" |
03 |
xmlns="http://java.sun.com/xml/ns/j2ee" |
04 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
05 |
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee |
06 |
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> |
08 |
<welcome-file>index.jsp</welcome-file> |
11 |
<servlet-name>LoginSerlvet</servlet-name> |
12 |
<servlet-class>com.cn.LoginSerlvet</servlet-class> |
15 |
<servlet-name>LoginSerlvet</servlet-name> |
16 |
<url-pattern>/login/servlet</url-pattern> |
002 |
mysql>CREATE DATABASE demo; |
003 |
mysql>CREATE TABLE user( |
004 |
id INT NOT NULL AUTO_INCREMENT, |
005 |
username VARCHAR(50) NOT NULL, |
006 |
password VARCHAR(20) NOT NULL, |
010 |
mysql>insert into user(id,username,password) values(default,'admin','admin'); |
011 |
mysql>insert into user(id,username,password) values(default,'huzjtech','123456'); |
013 |
创建验证用户的类UserValidation,他的功能是通过用户的输入数据到数据库中查找该用户是否存在: |
014 |
[java] view plaincopy |
015 |
package huzj.login.jdbc; |
017 |
import java.sql.Connection; |
018 |
import java.sql.DriverManager; |
019 |
import java.sql.PreparedStatement; |
020 |
import java.sql.ResultSet; |
021 |
import java.sql.SQLException; |
022 |
public class UserValidation { |
023 |
private String username; |
024 |
public void setUsername(String username) { |
025 |
this.username = username; |
027 |
public void setPassword(String password) { |
028 |
this.password = password; |
030 |
private String password; |
031 |
public boolean validate() throws SQLException, ClassNotFoundException { |
032 |
boolean flag = false; |
033 |
Class.forName("com.mysql.jdbc.Driver"); |
034 |
String url = "jdbc:mysql://localhost:3306/demo"; |
035 |
Connection conn = DriverManager.getConnection(url, "root", "password"); |
036 |
PreparedStatement ps = conn |
037 |
.prepareStatement("select * from user u where u.username=? and u.password=?"); |
038 |
ps.setString(1, username); |
039 |
ps.setString(2, password); |
040 |
ResultSet rs = ps.executeQuery(); |
047 |
创建一个servlet 完成分发LoginServlet; |
048 |
[java] view plaincopy |
049 |
package huzj.login.control; |
050 |
import huzj.login.entity.User; |
051 |
import huzj.login.jdbc.UserValidation; |
052 |
import java.io.IOException; |
053 |
import java.sql.SQLException; |
054 |
import javax.servlet.ServletException; |
055 |
import javax.servlet.http.HttpServlet; |
056 |
import javax.servlet.http.HttpServletRequest; |
057 |
import javax.servlet.http.HttpServletResponse; |
058 |
import org.apache.commons.logging.Log; |
059 |
import org.apache.commons.logging.LogFactory; |
060 |
public class LoginServlet extends HttpServlet { |
061 |
public void service(HttpServletRequest request, HttpServletResponse response) |
062 |
throws ServletException, IOException { |
063 |
Log logger = LogFactory.getLog(getClass()); |
064 |
logger.info("process this ...."); |
065 |
String username = request.getParameter("username"); |
066 |
String password = request.getParameter("password"); |
067 |
UserValidation uv = new UserValidation(); |
068 |
uv.setUsername(username); |
069 |
uv.setPassword(password); |
070 |
String url1 = "/loginsuccess.jsp"; |
071 |
String url2 = "/login.jsp"; |
073 |
if (uv.validate() == true) { |
074 |
request.setAttribute("username", username); |
075 |
getServletContext().getRequestDispatcher(url1).forward(request, |
078 |
// response.sendError(404, "Loging failure...!"); |
079 |
getServletContext().getRequestDispatcher(url2).forward(request, |
082 |
} catch (SQLException e) { |
083 |
// TODO Auto-generated catch block |
085 |
} catch (ClassNotFoundException e) { |
086 |
// TODO Auto-generated catch block |
093 |
<?xml version="1.0" encoding="UTF-8"?> |
094 |
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
095 |
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
096 |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" |
097 |
id="WebApp_ID" version="2.5"> |
098 |
<display-name>jdbc-first-app-login</display-name> |
100 |
<servlet-name>userlogin</servlet-name> |
101 |
<servlet-class>huzj.login.control.LoginServlet</servlet-class> |
104 |
<servlet-name>userlogin</servlet-name> |
105 |
<url-pattern>/login</url-pattern> |
108 |
<welcome-file>login.jsp</welcome-file> |
113 |
<%@ page language="java" contentType="text/html; charset=UTF-8" |
114 |
pageEncoding="UTF-8"%> |
115 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
118 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
119 |
<title>Insert title here</title> |
122 |
<form action="login"> |
123 |
userName:<input type="text" name="username"> |
124 |
userPassword:<input type="password" name="password"> |
125 |
<input type="submit" value="Submit"> |
129 |
登录成功页面loginsuccess.jsp: |
131 |
<%@ page language="java" contentType="text/html; charset=UTF-8" |
132 |
pageEncoding="UTF-8"%> |
133 |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
136 |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
137 |
<title>Insert title here</title> |
140 |
<h1>Login Success!</h1> |
141 |
<p>Hello,<b><%=request.getAttribute("username")%></b>!</p> |