这次涉及到四个文件:
登录页面:login.jsp
登录成功欢迎页面:success.jsp
登录失败页面:fail.jsp
Servlet处理文件:LoginServlet.java
其实还涉及到一个文件:web.xml,这个后面再说:
下面分别介绍这几个文件:
//--------------------------------------------------------------------------------------------------------------------
LoginServlet.java 内容:
package org.cai.servlet ;
import java.sql.* ;
import java.io.* ;
import javax.servlet.http.* ;
import javax.servlet.* ;
public class LoginServlet extends HttpServlet implements Servlet{
public LoginServlet(){
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
}
// protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
// System.out.println("-------------------------") ;
// doPost(request, response) ;
// }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html;charset=gb2312") ;
request.setCharacterEncoding("gb2312") ;
String result = "" ;
//获取用户名
String username = request.getParameter("username") ;
String psw = request.getParameter("password") ;
if ((username == "") || (username==null) || (username.length() > 20 )){
try{
result = "请输入用户名(不能超过20个字符)!" ;
request.setAttribute("message" ,result) ;
response.sendRedirect("login.jsp") ;
}catch(Exception e){
e.printStackTrace() ;
}
}
if ((psw == "") || (psw==null) || (psw.length() > 20 )){
try{
result = "请输入密码(不能超过20个字符)!" ;
request.setAttribute("message" ,result) ;
response.sendRedirect("login.jsp") ;
}catch(Exception e){
e.printStackTrace() ;
}
}
//登记JDBC驱动程序
try{
Class.forName("org.gjt.mm.mysql.Driver") ;
}catch(ClassNotFoundException e){
e.printStackTrace() ;
System.out.println("Class Not Found Exception . ") ;
}
//连接URL
String url ="jdbc:mysql://localhost:3306/learnJSP" ;
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
try{
conn = DriverManager.getConnection(url, "root", "root") ;
stmt = conn.createStatement() ;
//SQL语句
String sql ="select * from userInfo where username='"+username+"' and userpsw= '"+psw+"'" ;
rs = stmt.executeQuery(sql) ;//返回查询结果
}catch(SQLException e){
e.printStackTrace() ;
}
HttpSession session = request.getSession() ;
session.setAttribute("username", username) ;
//System.out.println("+++++++++++++++++++++++"+ username) ;
try{
if (rs.next()){ //如果记录集非空,表明有匹配的用户名和密码,登陆成功
// 登录成功后将username设置为session变量的username
// 这样在后面就可以通过 session.getAttribute("username") 来获取用户名,
// 同时这样还可以作为用户登录与否的判断依据
session.setAttribute("age",rs.getString("age")) ;
session.setAttribute("sex",rs.getString("sex")) ;
session.setAttribute("weight",rs.getString("weight")) ;
response.sendRedirect("success.jsp") ;
}else{
session.setAttribute("message", "用户名或密码不匹配。");
response.sendRedirect("fail.jsp") ;
}
}catch(SQLException e){
e.printStackTrace() ;
}
}
private static final long serialVersionUID = 1L;
}
//--------------------------------------------------------------------------------------------------------------------
login.jsp内容:
登陆用户登录
用户名: | |
密 码: | |
function validateLogin(){
var sUserName = document.frmLogin.username.value ;
var sPassword = document.frmLogin.password.value ;
if ((sUserName =="") || (sUserName=="Your name")){
alert("请输入用户名!");
return false ;
}
if ((sPassword =="") || (sPassword=="Your password")){
alert("请输入密码!");
return false ;
}
}
//--------------------------------------------------------------------------------------------------------------------
success.jsp 内容;
欢迎String userName = (String)session.getAttribute("username") ;
String age = (String)session.getAttribute("age") ;
String weight = (String)session.getAttribute("weight") ;
String sex = (String)session.getAttribute("sex") ;
System.out.println("性别:A" + sex +"A") ;
if (sex.trim().equals("M")) {
sex ="男" ;
}else{
sex = "女" ;
}
%>
//--------------------------------------------------------------------------------------------------------------------
fail.jsp 内容:
登陆失败String userName = (String)session.getAttribute("username") ;
String msg = (String)session.getAttribute("message") ;
%>
对不起,登陆失败!
原因:
5秒后将返回登陆界面。
response.setHeader("Refresh","5;URL=/learnJSP/login.jsp");
%>
//--------------------------------------------------------------------------------------------------------------------
mysql数据库中需要创建数据库learnJSP,其中创建表userInfo 创建表语句为:
create database learnJSP ;
use learnJSP
create table userInfo(id int not null primary key,username char(20) not null,userpsw char(20) ,age int ,weight int ,sex enum('M','F')) ;
需要把连接mysql的jar包放到项目的lib目录中
//--------------------------------------------------------------------------------------------------------------------
web.xml 内容:
/p>
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
index.jsp
LoginServlet
org.cai.servlet.LoginServlet
LoginServlet
/LoginServlet
//--------------------------------------------------------------------------------------------------------------------