涉及以下几个文件:
1。登录页面 login.jsp
2,成功跳转页面 success.jsp
3,失败跳转页面 fail.jsp
4,servlet 处理类 LoginTestServlet.java
5,配置文件 web.xml
--------------------------------------------- 依次看代码 --------------------------------------------
LoginTestServlet.java 内容
package com.test;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
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 javax.servlet.http.HttpSession;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
/**
* Servlet implementation class LoginTestServlet
*/
@WebServlet("/LoginTestServlet")
public class LoginTestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginTestServlet() {
super();
}
/**
* @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 {
response.setContentType("text/heml;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");
return;
} catch (Exception e) {
e.printStackTrace();
}
}
if(psw == "" || psw == null || psw.length() > 20){
try {
result = "请输入password(不能超过20个字符)";
request.setAttribute("message", result);
response.sendRedirect("login.jsp");
return;
} catch (Exception e) {
e.printStackTrace();
}
}
//登记JDBC驱动程序
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
System.out.print("Class Not Found Exception");
}
//链接URL
String url = "jdbc:mysql://localhost:3306/demo";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
conn = (Connection) DriverManager.getConnection(url,"root","root");
stmt = (Statement) conn.createStatement();
String sql = "select * from userInfo where username='"+username+"' and userpsw= '"+psw+"'";
rs = stmt.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
}
HttpSession session = request.getSession();
session.setAttribute("username", username);
try {
if(rs.next()){
session.setAttribute("age",rs.getString("age")) ;
session.setAttribute("sex",rs.getString("sex")) ;
session.setAttribute("weight",rs.getString("weight")) ;
response.sendRedirect("success.jsp") ;
return;
}else{
session.setAttribute("message", "用户名或password不匹配。
");
response.sendRedirect("fail.jsp") ;
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
login.jsp 内容
pageEncoding="UTF-8"%>
用户登录
用户登录
function validateLogin(){
var sUserName = document.frmLogin.username.value;
var sPassword = document.frmLogin.psssword.value;
if (sUserName ==""){
alert("请输入用户名!");
return false ;
}
if (sPassword ==""){
alert("请输入密码!");
return false ;
}
}
success.jsp 内容
pageEncoding="UTF-8"%>
Insert title here
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 内容
pageEncoding="UTF-8"%>
Insert title here
String userName = (String) session.getAttribute("username");
String msg = (String) session.getAttribute("message");
%>
对不起。登陆失败!原因:
5秒后将返回登陆界面。
response.setHeader("Refresh", "5;URL=/webDemo/login.jsp");
%>
-------------------------------------------------------配置数据库------------------------------------------------------
mysql数据库中须要创建数据库demo,当中创建表userInfo 创建表语句为:
create databasedemo;
use demo
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 内容
xmlns="/xml/ns/javaee"
xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
webDemo
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
LoginTestServlet
com.test.LoginTestServlet
LoginTestServlet
/loginTestServlet