Servlet实践 2-2

Categories: Java; Tagged with: ; @ February 13th, 2009 20:24

Login设计

当Request进入时, 先通过request.getSession(false)检查Session是否存在, 如果存在则表明已经登录,则通过sendRedirect(Calc.URL_MAIN)跳转到主页面
Session不存在时, 通过request.getParameter()方法获得输入的用于名及密码, 通过ServletContext的get Parameter()方法获得用户名及密码, 如果相互都equals, 则增加Session, 登录成功, 转入主页面. 如果不equals, 则提示输入有误, 重新输入
重要方法:

response.sendRedirect();
request.getParameter();
getServletContext().getAttribute();

package com.insprise.servletStu;
//省略import
public class Login extends HttpServlet {
      public static final String USER_NAME = "userName";
	public static final String PASS_WORD = "passWord";
	private static final long serialVersionUID = 1L;
	private static final String INPUT_USER_NAME = "inputUserName";
	private static final String INPUT_PASSWORD = "inputPassWord";
	private String passWord;
      private String name;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		name = (String) getServletContext().getAttribute(USER_NAME);
		passWord = (String) getServletContext().getAttribute(PASS_WORD);
		HttpSession session = request.getSession(false);
		if (session != null) {
			response.sendRedirect(Calc.URL_MAIN);//如果已经登录后再次打开登录页面, 跳转到主界面
			return;
		} else {
			response.setContentType("text/html");
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();

			if (request.getParameter(INPUT_USER_NAME) != null) {
				String inputUserName = request.getParameter(INPUT_USER_NAME);
				String inputPassWord = request.getParameter(INPUT_PASSWORD);
				if (inputUserName.equals(USER_NAME) && inputPassWord.equals(passWord)) {
					request.getSession().setAttribute(Calc.CURRENT_USER, USER_NAME);
					response.sendRedirect(Calc.URL_MAIN);
					return;
				} else {
					//登录失败
				}
			} else {
				//省略若干println…
	}
	//省略doPost
}

 

 

CalcListener实现

该类实现ServletContextListener接口, 在Application初始化时, contextInitialized(ServletContextEvent sce)方法会被自动呼叫, 在此之后, Filter与Servlet开始初始化.

使用getServletContext().getInitParameter()方法获得web.xml中<context-param>标签中的内容.

使用ServletContext的setAttribute方法为获得用户名及密码, 并作为Attribute加入到ServletContext中

重要方法:

sce.getServletContext();

ServletContext.getInitParameter();

ServletContext. setAttribute();

public class CalcListener implements ServletContextListener {

private static Logger log = Logger.getLogger(CalcListener.class); 

public void contextInitialized(ServletContextEvent sce) {

log.info("Context Started!" + sce.getServletContext());

ServletContext context = sce.getServletContext();

Calc.MAX_CALCULATION_TIMES_PER_SESSION = Integer.parseInt(context.getInitParameter(Calc.MAX_CALCULATIONAS_PER_SESSION));

Calc.MAX_CALCULATION_TIMES_PER_SESSION = Integer.parseInt(context.getInitParameter(Calc.MAX_CALCULATIONAS_PER_SESSION));

//读取到用户名及密码, 加入到Context Attribute中

context.setAttribute(Login.USER_NAME, new String(context.getInitParameter(Login.USER_NAME)));

context.setAttribute(Login.PASS_WORD, new String(context.getInitParameter(Login.PASS_WORD)));
}

}

 

2.4 抓图

clip_image002 clip_image004

clip_image006



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.