今天又把jsp注册登录做了一下 从index.jsp写表单开始
action="user.do?method=login" 写注册 需要注意给用户名 密码添加name属性
写完第一个jsp文件 再写servlet 新建new->package->(设置包名为com.servlet)->新建servlet文件
需要注意可以将which method stubs would you like to create?下面的√全取消
设置servlet名字为UserServlet后点击next->设置Servlet/JSP Mapping URL为在原来新建的jsp文件写的user.do就行了
这是我写的UserServlet;
package com.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 com.dao.UserDAO;
import com.pojo.User;
public class UserServlet extends HttpServlet {
private UserDAO userdao = new UserDAO();
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String method = request.getParameter("method");
if("login".equals(method)){
doLogin(request , response);
}
if("regist".equals(method)){
doRegist(request, response);
}
}
private void doRegist(HttpServletRequest request,
HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String repassword = request.getParameter("repassword");
String code = request.getParameter("code");
if(!password.equals(repassword)){
response.getWriter().print("<script>alert('两次密码不一致!');location='regist.jsp'</script>");
return ;
}
String realCode = (String) request.getSession().getAttribute("code");
if(!code.equalsIgnoreCase(realCode)){
response.getWriter().print("<script>alert('验证码不正确!');location='regist.jsp'</script>");
return ;
}
int n = userdao.regist(username, passw