JSP代码:
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=gbk" pageEncoding="gbk"%>
<%
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="CSS/register.css">
<script type="text/javascript" src="js/register.js">
</script>
<!--
<script type="text/javascript" src="js/ajax.js">
</script>
-->
</head>
<body>
<div id="register">
<div id="title"></div>
<div id="body">
<form id="myFormRegister" name="myFormRegister" method="post"
action="javascript:register()" onsubmit="return checkAll()">
<table>
<tr>
<td>
<label for="name">
用户名:
</label>
</td>
<td>
<input type="text" id="name" name="name" class="texts"
onblur="checkUserExists()" />
</td>
<td>
<span id="nameInfo"></span>
</td>
</tr>
<tr>
<td>
<label for="pwd">
密 码:
</label>
</td>
<td>
<input type="password" id="pwd" name="pwd" class="texts"
onfocus="pwdOnfocus()" onblur="checkPwd()" />
</td>
<td>
<span id="pwdInfo"></span>
</td>
</tr>
<tr>
<td>
<label for="rePwd">
确认密码:
</label>
</td>
<td>
<input type="password" id="rePwd" name="rePwd" class="texts"
onblur="checkRepwd()" />
</td>
<td>
<span id="rePwdInfo"></span>
</td>
</tr>
<tr>
<td>
<label for="email">
邮 箱:
</label>
</td>
<td>
<input type="text" id="email" name="email" class="texts"
onfocus="emailOnfocus()" onblur="checkEmail()" />
</td>
<td>
<span id="emailInfo"></span>
</td>
</tr>
</table>
<p>
<input type="submit" id="reg" name="reg" value="" />
<input type="reset" id="reset" name="reset" value=""
onclick="resetInfo()" />
</p>
<p>
<a href="index.jsp"><img src="images/sms_btn_goback.png" /> </a>
</p>
</form>
</div>
</div>
</body>
</html>
Servlet代码:
package com.control;
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.biz.UserDaoImpl;
import com.model.entity.User;
@SuppressWarnings("serial")
public class RegisterServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public RegisterServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
doPost(request, response);
}
/**
* 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 {
// 设置输入文本为中文
request.setCharacterEncoding("gbk");
// 设置输出文本为中文
response.setCharacterEncoding("gbk");
response.setContentType("text/html; charset=gbk");
PrintWriter out = response.getWriter();
String type = request.getParameter("type");
System.out.println(type);
// 实例化用户对象
User user = new User();
if (type.equals("checkName")) {
// 获取用户名
String name = request.getParameter("userName");
user.setUserName(name);
int result = new UserDaoImpl().getUserCount(user);
System.out.println("this is line 93:>>>>");
if (result == 0) {
out.print("用户名合法!");
} else {
out.print("该用户已存在!");
}
} else if ("register".equals(type)) {
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String email = request.getParameter("email");
user.setUserName(name);
user.setPassword(pwd);
user.setEmail(email);
int result = new UserDaoImpl().addUser(user);
if (result == 1) {
out.print("恭喜你!注册成功!");// 就这句话无法获取
request.getRequestDispatcher("index.jsp").forward(request,
response);
} else {
out.print("很抱歉,注册失败!");
}
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
|