JSP+JavaBean+Servlet登录系统

//login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'login.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
<form name="form1" action="UserServlet.do?method=login" method="post">
<table width="200" border="1">
<tr>
<td colspan="2">登录窗口</td>
<tr>
<td>用户名</td>
<td><input type="text" name="username" size="10"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" size="10"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="登录"> <a
href="register.jsp">注册新用户</a></td>
</tr>
</table>
</form>

</body>
</html>

//register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'register.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
<form name="form1" action="UserServlet.do?method=register" method="post">
<table width="200" border="1">
<tr>
<td colspan="2">注册窗口</td>
<tr>
<td>用户名</td>
<td><input type="text" name="username" size="10"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password1" size="10"></td>
</tr>
<tr>
<td>确认密码</td>
<td><input type="password" name="password2" size="10"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" size="10"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="提交注册"> <a
href="login.jsp">返回</a></td>
</tr>
</table>
</form>

</body>
</html>

//logout.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'welcome.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>
<table width="100%">
<tr>
<td><img src="images/logo4.png"></td>
<td><img src="images/logo2.png" height="90"></td>
</tr>
<tr>
<td colspan="2">
<hr>
</td>
</tr>
<tr>
<td>
<table>
<tr>
<td><a href="welcome.jsp">Main</a></td>
</tr>
<tr>
<td><a href="menu1.jsp">Menu1</a></td>
</tr>
<tr>
<td><a href="menu2.jsp">Menu2</a></td>
</tr>
<tr>
<td><a href="menu3.jsp">Menu3</a></td>
</tr>
<tr>
<td><a href="menu4.jsp">Menu4</a></td>
</tr>
<tr>
<td><a href="menu5.jsp">Menu5</a></td>
</tr>
<tr>
<td><a href="menu6.jsp">Menu6</a></td>
</tr>
<tr>
<td><a href="menu7.jsp">Menu7</a></td>
</tr>
<tr>
<td><a href="menu8.jsp">Menu8</a></td>
</tr>
</table>
</td>
<td>
<form name="form1" action="UserServlet.do?method=logout" method="post">
<table width="200" border="5">
<tr>
<td colspan="2">登录成功</td>
<tr>
<td>欢迎你,</td>
<td><%=(String) session.getAttribute("username")%></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="退出"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>







</body>
</html>

//下面是JavaBeans文件
//DBAccess.java
package com.demo.javabean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBAccess {

private String drv = "org.gjt.mm.mysql.Driver";

private String url = "jdbc:mysql://localhost:3306/demo";

private String usr = "root";

private String pwd = "root";

private Connection conn = null;

private Statement stm = null;

private ResultSet rs = null;

public boolean createConn() {
boolean b = false;
try {
Class.forName(drv).newInstance();
conn = DriverManager.getConnection(url, usr, pwd);
b = true;
} catch (SQLException e) {
} catch (ClassNotFoundException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
return b;
}

public boolean update(String sql) {
boolean b = false;
try {
stm = conn.createStatement();
stm.execute(sql);
b = true;
} catch (Exception e) {
System.out.println(e.toString());
}
return b;
}

public void query(String sql) {
try {
stm = conn.createStatement();
rs = stm.executeQuery(sql);
} catch (Exception e) {
}
}

public boolean next() {
boolean b = false;
try {
if(rs.next())b = true;
} catch (Exception e) {
}
return b;
}

public String getValue(String field) {
String value = null;
try {
if(rs!=null)value = rs.getString(field);
} catch (Exception e) {
}
return value;
}

public void closeConn() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}

public void closeStm() {
try {
if (stm != null)
stm.close();
} catch (SQLException e) {
}
}

public void closeRs() {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
}
}

public Connection getConn() {
return conn;
}

public void setConn(Connection conn) {
this.conn = conn;
}

public String getDrv() {
return drv;
}

public void setDrv(String drv) {
this.drv = drv;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

public ResultSet getRs() {
return rs;
}

public void setRs(ResultSet rs) {
this.rs = rs;
}

public Statement getStm() {
return stm;
}

public void setStm(Statement stm) {
this.stm = stm;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getUsr() {
return usr;
}

public void setUsr(String usr) {
this.usr = usr;
}
}

//UserBean.java
package com.demo.javabean;

public class UserBean {

public boolean valid(String username, String password) {
boolean isValid = false;
DBAccess db = new DBAccess();
if(db.createConn()) {
String sql = "select * from user where username='"+username+"' and password='"+password+"'";
db.query(sql);
if(db.next()) {
isValid = true;
}
db.closeRs();
db.closeStm();
db.closeConn();
}
return isValid;
}

public boolean isExist(String username) {
boolean isExist = false;
DBAccess db = new DBAccess();
if(db.createConn()) {
String sql = "select * from user where username='"+username+"'";
db.query(sql);
if(db.next()) {
isExist = true;
}
db.closeRs();
db.closeStm();
db.closeConn();
}
return isExist;
}

public void add(String username, String password, String email) {
DBAccess db = new DBAccess();
if(db.createConn()) {
String sql = "insert into user(username,password,email) values('"+username+"','"+password+"','"+email+"')";
db.update(sql);
db.closeStm();
db.closeConn();
}
}

}

//UserServlet.java
package com.demo.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 javax.servlet.http.HttpSession;

import com.demo.javabean.UserBean;

public class UserServlet extends HttpServlet {

protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String method = (String)request.getParameter("method");
if(method==null) {
PrintWriter out = response.getWriter();
out.println("invalid request!");
} else if(method.equals("login")) {
Login(request, response);
} else if(method.equals("logout")) {
Logout(request, response);
} else if(method.equals("register")) {
Register(request, response);
}
}

protected void Login(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// get parameters
String username = request.getParameter("username");
String password = request.getParameter("password");

// check null
if (username == null || password == null) {
response.sendRedirect("login.jsp");
return;
}

// validate
UserBean userBean = new UserBean();
boolean isValid = userBean.valid(username, password);

if (isValid) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
return;
} else {
response.sendRedirect("login.jsp");
return;
}
}

protected void Logout(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.removeAttribute("username");
response.sendRedirect("login.jsp");
}

protected void Register(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// get parameters
String username = request.getParameter("username");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
String email = request.getParameter("email");

// check null
if (username == null || password1 == null || password2 == null || !password1.equals(password2)) {
response.sendRedirect("register.jsp");
return;
}

// validate
UserBean userBean = new UserBean();
boolean isExist = userBean.isExist(username);
if(!isExist) {
userBean.add(username, password1, email);
response.sendRedirect("login.jsp");
return;
} else {
response.sendRedirect("register.jsp");
return;
}
}

}



//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>UserServlet</servlet-name>
    <servlet-class>com.demo.servlet.UserServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>UserServlet</servlet-name>
    <url-pattern>/UserServlet.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


这里还有上传的源代码。

//数据库创建语句:
create  database demo ;

use demo;

create table user(
username  varchar(255)  not  null,
password varchar(255) ,
email varchar(255)
);

你可能感兴趣的:(java,sql,mysql,jsp,servlet)