jsp mvc设计模式的登陆注册

项目结构:


数据库结构:





数据库代码:

-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL auto_increment,
  `user` varchar(50) NOT NULL,
  `pwd` varchar(50) NOT NULL,
  `name` varchar(50) NOT NULL,
  `sex` varchar(50) NOT NULL,
  `age` int(50) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;


index.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>欢迎来到学生管理系统</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>
<div align="center"> <font size="+2" color="#FF6633">用户登录</font>
</div>
<form id="form1" name="form1" method="post" action="loginServlet">
<table width="357" border="0" align="center">
<tr>
<td width="128">用户名:</td>
<td width="219"><label>
<input name="user" type="text" id="user" />
</label></td>
</tr>
<tr>
<td>密 码:</td>
<td><label>
<input name="pwd" type="password" id="pwd" />
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="Submit" value="登录" />
</label></td>
<td><label><a href="addUser.jsp">用户注册</a></label></td>
</tr>
</table>
<p> </p>
</form>
</body>
</html>

addUser.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>用户注册</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>
<p align="center"><font size="+3" color="#FF3300">用户注册</font></p>
<form id="form1" name="form1" method="post" action="addUserServlet">
<table width="340" border="0" align="center">
<tr>
<td width="141">用户名:</td>
<td width="189"><label>
<input name="user" type="text" id="user" />
</label></td>
</tr>
<tr>
<td>密码:</td>
<td><label>
<input name="pwd" type="password" id="pwd" />
</label></td>
</tr>
<tr>
<td>重输密码:</td>
<td><label>
<input name="pwd1" type="password" id="pwd1" />
</label></td>
</tr>
<tr>
<td>姓名:</td>
<td><label>
<input name="name" type="text" id="name" />
</label></td>
</tr>
<tr>
<td>性别:</td>
<td><label>
<input name="sex" type="radio" value="男" checked="checked" />
男
<input type="radio" name="sex" value="女" />
女</label></td>
</tr>
<tr>
<td>年龄:</td>
<td><label>
<input name="age" type="text" id="age" />
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="Submit" value="提交" />
</label></td>
<td><label>
<input type="reset" name="Submit2" value="重置" />
</label></td>
</tr>
</table>
</form>
</body>
</html>

info.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
<h1>注册成功!</h1>
</body>
</html>

MyJsp.jsp

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
<h1>登陆成功!</h1>
</body>
</html>
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>zxl</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>addUserServlet</display-name>
    <servlet-name>addUserServlet</servlet-name>
    <servlet-class>servlet.addUserServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>addUserServlet</servlet-name>
    <url-pattern>/addUserServlet</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>loginServlet</display-name>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>servlet.loginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>
</web-app>
User.java

package bean;

public class User {
private String user;
private String pwd;
private String name;
private String sex;
private int age;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

Dao.java

package dao;

import getConnection.GetConnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import bean.User;

public class Dao {
	private Connection conn;
	private PreparedStatement pstat;
	String sql = "";

	/**
	 * 
	 * 用户登录
	 */
	public boolean logoin(User user) throws SQLException {
		conn = GetConnection.getConnection();
		boolean i = false;
		sql = "select * from user where user=? and pwd=?";

		pstat = conn.prepareStatement(sql);

		pstat.setString(1, user.getUser());
		pstat.setString(2, user.getPwd());

		ResultSet rs1 = (ResultSet) pstat.executeQuery();
		if (rs1.next()) {
			i = true;
			rs1.close();
			pstat.close();
		} else {
			i = false;
			rs1.close();
			pstat.close();
		}
		conn.close();
		return i;
	}

	/**
	 * 用户注册
	 */
	public void addUser(User user) {
		conn = GetConnection.getConnection();
		sql = "insert into user(user,pwd,name,sex,age) values(?,?,?,?,?)";
		try {
			pstat = conn.prepareStatement(sql);
			pstat.setString(1, user.getUser());
			pstat.setString(2, user.getPwd());
			pstat.setString(3, user.getName());
			pstat.setString(4, user.getSex());
			pstat.setInt(5, user.getAge());
			pstat.executeUpdate();
			System.out.println("run1");
			pstat.close();
			conn.close();

		} catch (SQLException e) {
			e.printStackTrace();
		}

	}
}
GetConnection.java

package getConnection;

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

/**
 * 
 * 用JDBC的方法获得数据库的连接
 * 
 */
public class GetConnection {
	// 通过静态方法注册驱动,获得连接
	public static Connection getConnection() {
		String driver = "com.mysql.jdbc.Driver";
		String url = "jdbc:mysql://localhost/student";
		Connection con = null;
		try {
			Class.forName(driver);
			try {
				con = DriverManager.getConnection(url, "root", "2009");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println("已获得数据库的连接");
		return con;
	}
	/*
	 * public static void main(String args[]){ getConnection(); }
	 */
}
addUserServlet.java

package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Dao;

import bean.User;

public class addUserServlet extends HttpServlet {

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String user1 = request.getParameter("user");
String pwd = request.getParameter("pwd");
String name = new String(request.getParameter("name").getBytes("ISO8859_1"),"utf-8");
String sex = new String(request.getParameter("sex").getBytes("ISO8859_1"),"utf-8");
String age1 = request.getParameter("age");
User user = new User();
user.setUser(user1);
user.setPwd(pwd);
user.setName(name);
user.setSex(sex);
int age;
if(age1!=null)
{
age = Integer.parseInt(age1);
user.setAge(age);
}
Dao dao = new Dao();
dao.addUser(user);
/* request.setAttribute("info",new String("<br><br><center><h1><font color=red>添加成功!恭喜!!" +
"</font></h1></center><br>"));
request.setAttribute("id", new String("a"));
request.setAttribute("denglu",new String("<br><br><center><a href = /jspDev/index.jsp target =_parent>登陆</href></center><br>"));*/


request.getRequestDispatcher("info.jsp").forward(request,response);
}

public void init() throws ServletException {
// Put your code here
}

}

loginServlet.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.User;
import dao.Dao;

public class loginServlet extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = new String (request.getParameter("user").getBytes("ISO8859_1"), "GBK");
String pwd = new String (request.getParameter("pwd").getBytes("ISO8859_1"),"UTF-8");
User user = new User();
user.setUser(name);
user.setPwd(pwd);
Dao dao = new Dao();
boolean isLogin;
try
{
isLogin = dao.logoin(user);

if(isLogin)
{
response.sendRedirect("MyJsp.jsp");
}else{
response.sendRedirect("index.jsp");
}

} catch (SQLException e)
{
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void init() throws ServletException {
}

}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值