标签:读取 auth ESS character 取数据 代码 jdbc comm 驱动
首先要建立好数据库。
好了,先写好两个静态页面,分别负责登录和注册。
代码清单1:login.jsp(登录页面)
<%--
Document : login
Created on : 2013-8-29, 17:14:30
Author : Barudisshu
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form action="login.do" method="post">
<table>
<tr>
<td><label for="user">用户名:</label></td>
<td><input type="text" id="user" name="user" value=""></td>
</tr>
<tr>
<td><label for="password">密码:</label></td>
<td><input type="password" id="password" name="password" value=""></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="登录"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
代码清单2:register.jsp (注册页面)
<%--
Document : register
Created on : 2013-8-29, 17:14:50
Author : Barudisshu
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<div>
<form action="register.do" method="post">
<table>
<tr>
<td><label for="user">用户名:</label></td>
<td><input type="text" id="user" name="user" value=""></td>
</tr>
<tr>
<td><label for="password">密码:</label></td>
<td><input type="password" id="password" name="password" value=""></td>
</tr>
<tr>
<td><label for="resure">确认密码:</label></td>
<td><input type="password" id="resure" name="resure" value=""></td>
</tr>
<tr>
<td><label for="email">邮箱地址:</label></td>
<td><input type="email" id="password" name="email" value=""></td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="注册"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
建立数据库模型,主要实现数据库连接建立、执行查询、释放连接、判断等操作。
代码清单3:DataProvider.java (数据模型)
创建注册验证Servlet,将取得请求参数、验证请求参数,业务跳转。逻辑业务部分。
代码清单4:Register.java (注册)
下面分别建立注册成功与失败的视图。
代码清单5:Error.java (注册失败)
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Barudisshu
*/
public class Error extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<meta charset=\"utf-8\">");
out.println("<title>新增会员失败</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>新增会员失败</h1>");
out.println("<ul style=‘color:red;‘>");
List<String> errors = (List<String>) request.getAttribute("errors");
for (String error : errors) {
out.println("<li>" + error + "</li>");
}
out.println("</ul>");
out.println("<a href=‘register.html‘>返回注册页面</a>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
代码清单6:Success.java (注册成功)
注册成功后就可以登录了,登录需要进行相关验证操作。
代码清单7:Login.java (验证用户登录)
验证失败,则从新登录,通过sendRedirect()跳转到登录页面;验证成功,则进行下一步操作。
代码清单8:Member.java (登录成功)
代码清单9:web.xml (标准依赖部署文件)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>net.individuals.web.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>RegisterServlet</servlet-name>
<servlet-class>net.individuals.web.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Error</servlet-name>
<servlet-class>net.individuals.web.servlet.Error</servlet-class>
</servlet>
<servlet>
<servlet-name>Success</servlet-name>
<servlet-class>net.individuals.web.servlet.Success</servlet-class>
</servlet>
<servlet>
<servlet-name>Member</servlet-name>
<servlet-class>net.individuals.web.servlet.Member</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/register.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Error</servlet-name>
<url-pattern>/error.view</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Success</servlet-name>
<url-pattern>/success.view</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Member</servlet-name>
<url-pattern>/member.view</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
标签:读取 auth ESS character 取数据 代码 jdbc comm 驱动
原文地址:https://www.cnblogs.com/yangshuyuan1009/p/10193489.html