注册功能
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>My JSP 'index.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>
项目的根目录--》basePath: <%=basePath%>
<form action="<%=basePath%>/servlet/LoginServlet" method="get">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="userpwd"><br/>
<input type="submit" value="提交"> <a href="<%=basePath%>/register.jsp">去注册</a>
</form>
</body>
</html>
index.jsp中对应的LoginServlet
LoginServlet.java
package com.wpf.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;
public class LoginServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接受数据
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
//设置返回中文乱码解决
response.setContentType("text/html;charset=utf-8");
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("1".equals(username)&&userpwd.equals("1")){
out.print("登录成功");
}else{
out.print("<font color='red'>登录失败</font>");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
注册页面信息 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>注册信息</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>
<h1>用户信息注册</h1>
<form action="<%=basePath %>/servlet/RegisterServlet" method="POST">
用户名:<input name="username"><br>
密码:<input type="password" name="userpwd"><br>
签名:<textarea rows="5" cols="10" name="qm"></textarea><br>
<input type="submit" value="注册"><br>
</form>
</body>
</html>
register.jsp对应的RegisterServlet
RegisterServlet.java
package com.wpf.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 com.wpf.dao.UsersDao;
import com.wpf.model.Users;
public class RegisterServlet extends HttpServlet {
/**
1.获取前台传递的数据
2.把数据存放到数据库中
3.如果插入成功,呢么显示注册成功 并 可以跳转到 登录页面,如果插入失败,那么显示 注册失败,
4.思考一下, 如果 插入功能,能不能直接跳转到登录页面,
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//接受请求的信息
String username = request.getParameter("username");
//如何解决 Servlet doGet请求的 中文乱码问题
username = new String(username.getBytes("ISO-8859-1"), "UTF-8");
String userpwd = request.getParameter("userpwd");
userpwd = new String(userpwd.getBytes("ISO-8859-1"), "UTF-8");
String qm = request.getParameter("qm");
qm = new String(qm.getBytes("ISO-8859-1"), "UTF-8");
//把数据存放到容器中 也就是封装的model对象
Users user = new Users();
user.setUsername(username);
user.setUserpwd(userpwd);
user.setQm(qm);
System.out.println(user);
//数据存放到数据库中
UsersDao dao = new UsersDao();
int count = dao.insertUsers(user);
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//响应的信息
response.setContentType("text/html;charset=utf-8");
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(count>0){
out.println("注册成功! <a href='"+basePath+"/index.jsp'>去登录</a> ");
}else{
out.print("注册失败!");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* 表单 的提交数据一般提交到post方法中
*
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//如何解决 Servlet doPost请求的 中文乱码问题
request.setCharacterEncoding("UTF-8");
//接受请求的信息
String username = request.getParameter("username");
String userpwd = request.getParameter("userpwd");
String qm = request.getParameter("qm");
//把数据存放到容器中 也就是封装的model对象
Users user = new Users();
user.setUsername(username);
user.setUserpwd(userpwd);
user.setQm(qm);
System.out.println(user);
//数据存放到数据库中
UsersDao dao = new UsersDao();
int count = dao.insertUsers(user);
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//响应的信息
response.setContentType("text/html;charset=utf-8");
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(count>0){
out.println("POST 注册成功! <a href='"+basePath+"/index.jsp'>去登录</a> ");
}else{
out.print("注册失败!");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
最后为 生成 web.xml内容信息 (WebRoot–>WEB-INF–> web.xml)文件内容,此文件为自动生成的不要随意更改
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>LoginServlet</servlet-name>
<servlet-class>com.wpf.servlet.LoginServlet</servlet-class>
</servlet>
<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>RegisterServlet</servlet-name>
<servlet-class>com.wpf.servlet.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>RegisterServlet</servlet-name>
<url-pattern>/servlet/RegisterServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>