您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 用户登录代码--jsp
3.编写一个用户登录JavaWeb应用程序,要求使用Servelet监听器技术,当用户成功登录后跳转到欢迎页面,在欢迎页面上显示当前在线人员的登录名称。Login.jsp:%@pagelanguage=javacontentType=text/html;charset=UTF-8pageEncoding=UTF-8%htmlheadmetahttp-equiv=Content-Typecontent=text/html;charset=UTF-8title登录页面/title/headbodyformaction=LoginServletmethod=post用户名:inputtype=textname=usernamebrinputtype=submitvalue=提交/ inputtype=resetvalue=重置/br/form/body/htmlLoginServlet:packagecn.edu.qfnu.ch08.servlet;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/***ServletimplementationclassLoginServlet*/publicclassLoginServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicLoginServlet(){super();//TODOAuto-generatedconstructorstub}/***@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//获取用户名信息Stringusername=request.getParameter(username);//将用户名信息写入SessionHttpSessionsession=request.getSession();session.setAttribute(username,username);//将用户名信息写入ServletContextServletContextctx=this.getServletContext();ListonlineUserList=(List)ctx.getAttribute(onlineUserList);//第一个用户访问时,需要初始化onlineUserListif(onlineUserList==null){onlineUserList=newArrayList();}onlineUserList.add(username);//更新onlineUserListctx.setAttribute(onlineUserList,onlineUserList);//转到显示当前用户的Servletresponse.sendRedirect(ShowOnlineServlet);}/***@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}ShowOnlionServlet:ackagecn.edu.qfnu.ch08.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.List;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/***ServletimplementationclassShowOnlineServlet*/publicclassShowOnlineServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicShowOnlineServlet(){super();//TODOAuto-generatedconstructorstub}/***@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//获取当前用户的usernameHttpSessionsession=request.getSession();Stringusername=(String)session.getAttribute(username);response.setContentType(text/html;charset=utf-8);PrintWriterout=response.getWriter();out.print(html);out.print(head);out.print(title);out.print(显示当前在线的所有用户名);out.print(/title);out.print(/head);out.print(body);if(username!=null){//给出注销链接out.print(username+欢迎访问,ahref='LogoutServlet'注销/abr);}ServletContextctx=this.getServletContext();ListonlineUserList=(List)ctx.getAttribute(onlineUserList);if(onlineUserList!=null&&onlineUserList.size()0){out.print(当前的在线用户为:);//显示所有当前用户for(inti=0;ionlineUserList.size();i++){out.print((String)onlineUserList.get(i)+);}}else{out.print(当前没有任何用户访问!);}out.print(/body);out.print(/html);}/***@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doGet(request,response);}}LogoutServlet:packagecn.edu.qfnu.ch08.servlet;importjava.io.IOException;importjava.util.List;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/***ServletimplementationclassLogoutServlet*/publicclassLogoutServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicLogoutServlet(){super();//TODOAuto-generatedconstructorstub}/***@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//获取session对象HttpSessionsession=request.getSession();//获取当前用户名Stringusername=(String)session.getAttribute(username);//销毁sessionsession.invalidate();ServletContextctx=this.getServletContext();//在ServletContext中删除当前用户名ListonlineUserList=(List)ctx.getAttribute(onlineUserList);onlineUserList.remove(username);ctx.setAttribute(onlineUserList,onlineUserList);//重定向到ShowOnlineServletresponse.sendRedirect(ShowOnlineServlet);}/***@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoPost(HttpServletRequestrequest,HttpSe
本文标题:用户登录代码--jsp
链接地址:https://www.777doc.com/doc-5897951 .html