首先创建util包、service包、model包,并导入架包
在util包里创建DBUtil工具类
package org.liubingfeng.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(
"jdbc:sqlserver://localhost:1433;databaseName=practice0924", "sa", "123456");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {
try {
if (conn != null)
conn.close();
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
在model包里创建Practice类,储存信息模型
package org.liubingfeng.model;
public class Practice {
private Integer id;
private String userName;
private String pwd;
private String name;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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 Practice() {
super();
}
public Practice(Integer id, String userName, String pwd, String name,
String sex) {
super();
this.id = id;
this.userName = userName;
this.pwd = pwd;
this.name = name;
this.sex = sex;
}
public Practice(String userName, String pwd, String name, String sex) {
super();
this.userName = userName;
this.pwd = pwd;
this.name = name;
this.sex = sex;
}
@Override
public String toString() {
return "Practice [id=" + id + ", userName=" + userName + ", pwd=" + pwd
+ ", name=" + name + ", sex=" + sex + "]";
}
}
在reg.jsp里写入html代码,作为注册页面
<%@ 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 'reg.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">
<style type="text/css">
form {
text-align: center;
}
table {
margin: 0 auto;
}
.btn{
text-align: center;
}
.btnSize{
width: 65px;
}
</style>
</head>
<body>
<form action="reg" method="post">
<table border="1" cellspacing="0" cellpadding="0">
<h2>注册页面</h2>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td>姓名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="text" name="sex" /></td>
</tr>
<tr>
<td colspan="2" class="btn" ><input class="btnSize" type="submit" value="确认" /></td>
</tr>
</table>
</form>
</body>
</html>
在login.jsp里写入html代码,作为登录页面
<%@ 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">
<style type="text/css">
form {
text-align: center;
}
table {
margin: 0 auto;
}
.btn{
text-align: center;
}
.btnSize{
width: 65px;
}
</style>
</head>
<body>
<form action="login" method="post">
<table border="1" cellspacing="0" cellpadding="0">
<h2>登录页面</h2>
<tr>
<td>用户名:</td>
<td><input type="text" name="userName" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" /></td>
</tr>
<tr>
<td colspan="2" class="btn" ><input class="btnSize" type="submit" value="确认" /></td>
</tr>
</table>
</form>
</body>
</html>
在select.jsp里写入html代码,作为详情页面
<%@ 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 'select.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">
</head>
<body>
<form action="select" method="post"></form>
</body>
</html>
配置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">
<display-name></display-name>
<servlet>
<servlet-name>select</servlet-name>
<servlet-class>org.liubingfeng.service.SelectServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>org.liubingfeng.service.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>org.liubingfeng.service.RegServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>select</servlet-name>
<url-pattern>/select</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>reg</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>reg.jsp</welcome-file>
</welcome-file-list>
</web-app>
创建RegServlet注册页面
package org.liubingfeng.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.liubingfeng.util.DBUtil;
public class RegServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
boolean isTrue = false;
Connection conn = DBUtil.getConn();
String sql = "insert into Practice (userName,pwd,name,sex) values (?,?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
ps.setString(3, name);
ps.setString(4, sex);
int count = ps.executeUpdate();
if (count > 0) {
isTrue = true;
} else {
isTrue = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.close(conn, ps, null);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if (isTrue) {
response.sendRedirect("login.jsp");
} else {
response.sendRedirect("reg.jsp");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
创建LoginServlet登录页面
package org.liubingfeng.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.liubingfeng.util.DBUtil;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String userName = request.getParameter("userName");
String pwd = request.getParameter("pwd");
boolean isTrue = false;
Connection conn = DBUtil.getConn();
String sql = "select * from Practice where userName = ? and pwd = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, pwd);
rs = ps.executeQuery();
if (rs.next()) {
isTrue = true;
} else {
isTrue = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.close(conn, ps, rs);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
if (isTrue) {
response.sendRedirect("select");
} else {
response.sendRedirect("login.jsp");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
创建SelectServlet详情页面
package org.liubingfeng.service;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.liubingfeng.model.Practice;
import org.liubingfeng.util.DBUtil;
public class SelectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println("<style type=text/css>"
+ "h1{text-align: center;}"
+ "table{margin: 0 auto; width: 500px;}"
+ ".temp{width: 100px; height: 60px; text-align: center;}"
+ "</style>");
out.println(" <BODY>");
out.println("<h1>详情页面</h1>");
out.println("<table border=\"1\" cellspacing=\"0\" >");
out.println("<tr>"
+ "<th class=temp>编号</th>"
+ "<th class=temp>用户名</th>"
+ "<th class=temp>密码</th>"
+ "<th class=temp>姓名</th>"
+ "<th class=temp>性别</th>"
+"</tr>");
Connection conn = DBUtil.getConn();
String sql = "select * from Practice";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
out.println("<tr>"
+ "<td class=temp>" + rs.getInt("id") + "</td>"
+ "<td class=temp>" + rs.getString("userName") + "</td>"
+ "<td class=temp>" + rs.getString("pwd") + "</td>"
+ "<td class=temp>" + rs.getString("name") + "</td>"
+ "<td class=temp>" + rs.getString("sex") + "</td>"
+ "</tr>");
}
} catch (SQLException e) {
e.printStackTrace();
}
DBUtil.close(conn, ps, rs);
out.println("</table>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
注册页面效果图
登录页面效果图
详情页面效果图