前台页面与后台servlet实现登录的三种实现方式

第一种:action获取数据在servlet实现转发处理实现登录

前台页面


账号:

密码:



后台servlet:

String userid = request.getParameter("userId");
String userpwd = request.getParameter("userPwd");

if ("".equals(userid) || "".equals(userpwd)) {
response.sendRedirect("login.jsp");
}else if(null == userid && null == userpwd ) {
response.sendRedirect("login.jsp");
}else if("admin".equals(userid) && "123".equals(userpwd) ) {
response.sendRedirect("sucess.jsp");
}else {
response.sendRedirect("login.jsp");
}

第二种:获取参数在Jspye页面进行处理实现登录

后台:

response.setContentType("text/html;charset=utf-8");//设置http输出格式
        response.setCharacterEncoding("utf-8");//设置字符编码格式
        HttpSession session = request.getSession();
        String rec="";
String userName= request.getParameter("userName");
String userId= request.getParameter("userId");
if("".equals(userName)|"".equals(userId)|userName==null|userId==null) {
session.setAttribute("msg", "输入错误");
response.sendRedirect("NewFile1.jsp");
}else {
rec="欢迎"+userName+"回来!";
// rec=new String(rec.getBytes("iso8859-1"),"utf-8");
response.getWriter().write(rec);
}
}

前台


<%
String str=(String)session.getAttribute("msg");
if(str != null ){
out.write("");
}
%>








第三种:使用ajax进行处理实现登录

前台:

Insert title here




userName

userPwd







后台:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html; charset=UTF-8");
resp.setCharacterEncoding("UTF-8");
String userName =req.getParameter("userName");
String userPwd =req.getParameter("userPwd");
//   HttpSession session = req.getSession();
if(userName==null||userPwd==null){
System.out.println("111111111");
// session.setAttribute("errMsg", "您输入的姓名和密码有误");
// resp.sendRedirect("MyFirst.jsp");
resp.getWriter().write("账号密码不能为空!!!!");
}else if("".equals(userName)||"".equals(userPwd)) {
System.out.println("22222222");
// session.setAttribute("errMsg", "您输入的姓名和密码有误");
// resp.sendRedirect("MyFirst.jsp");
resp.getWriter().write("账号密码不能为空!!!!");
}else if("aaa".equals(userName)&&"aaa".equals(userPwd)) {
resp.getWriter().write("OK");
}else {
System.out.println("33333333");
// session.setAttribute("errMsg", "您输入的姓名和密码有误");
// resp.sendRedirect("MyFirst.jsp");
resp.getWriter().write("账号密码输入错误!!!!!");
}

}

你可能感兴趣的:(前台页面与后台servlet实现登录的三种实现方式)