servlet中处理用户非法登录页面

  比如我们现在有两个页面,一个是登录页面(login.java),一个是登录后的欢迎页面(wel.java),现在我们只有先让用户正常登录后才能允许进入wel.jsp页面(也就是不准用户直接进入wel.jsp),这时我们就得用到session技术。

 

//login.java如下

package cn.hnu;

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 testhtml extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  resp.setContentType("text/html;charset=gbk");
  try {
   PrintWriter pw = resp.getWriter();
   pw.println("<html>");
   pw.println("<head>");
   pw.println("<title>");
   pw.println("用户登录");
   pw.println("</title>");
   pw.println("</head>");
   pw.println("<body>");
   pw.println("<h1>用户登录</h1>");
   pw.println("<hr>");
   pw.println("<form method=post action=loginCl>");
   pw.println("用户名:<input type=text name=userName><br>");
   pw.println("密&nbsp&nbsp码:<input type=password name=password><br>");
   pw.println("<input type=submit value=登录>");
   pw.println("<input type=reset value=重置>");
   pw.println("</form>");
   pw.println("</body>");
   pw.println("</html>");
  } catch (Exception e) {
   e.printStackTrace();
   // TODO: handle exception
  }
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  this.doGet(req, resp);
 }
 

}

 

 

//loginCl.java如下,它主要用于负责页面的转发及用户登录验证

package cn.hnu;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class loginCl extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  String u = req.getParameter("userName");
  String p = req.getParameter("password");
  
  if(u.equals("fxk")&& p.equals("123")){
   HttpSession hs = req.getSession(true);
   hs.setAttribute("pass", "OK");
   resp.sendRedirect("wel");
  }else{
   resp.sendRedirect("login");
  }
  }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  this.doGet(req, resp);
 }

}

 

 

//wel.java如下,它主要用于用户正常登录后显示信息给用户

package cn.hnu;

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 javax.servlet.http.HttpSession;

public class Wel extends HttpServlet {

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  HttpSession hs = req.getSession();
  String s = (String)hs.getAttribute("pass");
  if(s == null){
   resp.sendRedirect("login");
  }
  PrintWriter pw = resp.getWriter();
  pw.write("welcome,hello");
 }

 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  // TODO Auto-generated method stub
  this.doGet(req, resp);
 }

}

你可能感兴趣的:(servlet中处理用户非法登录页面)