问题描述
我的CONTEXTLISTENER写成importjavax.servlet.ServletContextEvent;importjavax.servlet.ServletContextListener;importjavax.servlet.annotation.WebListener;importjavax.servlet.*;importjava.io.*;importjava.util.Map;importjava.util.TreeMap;/***ApplicationLifecycleListenerimplementationclassAppInitListener**/@WebListenerpublicclassAppInitListenerimplementsServletContextListener{/***Defaultconstructor.*/publicAppInitListener(){//TODOAuto-generatedconstructorstub}/***@seeServletContextListener#contextInitialized(ServletContextEvent)*/publicvoidcontextInitialized(ServletContextEventsce){//TODOAuto-generatedmethodstubServletContextcontext=sce.getServletContext();Map<String,String>users=newTreeMap<String,String>();FileInputStreamfis=null;try{fis=newFileInputStream(context.getRealPath("users.txt"));}catch(FileNotFoundExceptione1){//TODOAuto-generatedcatchblocke1.printStackTrace();}InputStreamReaderisr=newInputStreamReader(fis);BufferedReaderbr=newBufferedReader(isr);Strings=null;try{while((s=br.readLine())!=null){String[]sa=s.split(",");users.put(sa[0],sa[1]);}}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}context.setAttribute("users",users);}/***@seeServletContextListener#contextDestroyed(ServletContextEvent)*/publicvoidcontextDestroyed(ServletContextEventarg0){//TODOAuto-generatedmethodstub}}Sevlet写成importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Iterator;importjava.util.Map;importjava.util.Set;importjava.util.TreeMap;importjavax.servlet.ServletContext;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/***ServletimplementationclassLoginServlet*/@WebServlet("/LoginServlet")publicclassLoginServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/***@seeHttpServlet#HttpServlet()*/publicLoginServlet(){super();//TODOAuto-generatedconstructorstub}/***@seeHttpServlet#doGet(HttpServletRequestrequest,HttpServletResponseresponse)*/@SuppressWarnings("unchecked")protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstubMap<String,String>m=newTreeMap<String,String>();ServletContextcon=this.getServletContext();m=(TreeMap<String,String>)con.getAttribute("users");response.setContentType("text/html;charset=UTF-8");PrintWriterout=response.getWriter();out.println("<html><head><title>登陆界面</title></head>");Stringuser="";Stringpassword="";user=request.getParameter("user");password=request.getParameter("password");out.println("<body><form>用户名");out.println("<inputtype="text"name="user"/>");out.println("密码");out.println("<inputtype="password"name="password"/>");out.println("<inputtype="submit"value="登陆"></form>");Set<String>key=m.keySet();if(user==""||password=="")out.println("请输入信息");for(Iterator<String>it=key.iterator();it.hasNext();){user=(String)it.next();if(password.equals(m.get(user))){out.println("欢迎登陆");out.println("当前时间:"+System.currentTimeMillis());out.println("IP:"+request.getRemoteAddr());}}out.println("</body></html>");}/***@seeHttpServlet#doPost(HttpServletRequestrequest,HttpServletResponseresponse)*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{//TODOAuto-generatedmethodstub}}老是404错误。。新手。。刚学。。不知道哪错了
解决方案
解决方案二:
额。对了。user.txt里面保存了用户名和密码。事先输入好的aaa,111bbb,222ccc,333
解决方案三:
404就是找不到页面了,和数据应该没关系的。你看看是不是页面跳转路径写错了?
解决方案四:
看看你的路径对不对
解决方案五:
检查xml配制文件。
解决方案六:
你有重定向到另外个地址吗?