Java Web Learning Note 3

Source: Internet
Author: User

An experiment was done today:The servlet accesses the file under the Web-inf directory notice.html

At the end of the day, I guess the reason for the use of the Tomcat 8 version is that it's not going to work.

The current results are as follows:

The code and configuration are as follows:

 PackageCom.helloxr.servlet;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.util.Enumeration;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/** servlet accesses files under the Web-inf directory notice.html*/ Public classInitparamservletextendsHttpServlet {Private Static Final LongSerialversionuid = 6272811112212L;  Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {request.setcharacterencoding ("UTF-8");//set the request encoding methodResponse.setcharacterencoding ("UTF-8");//Setting the response encoding methodResponse.setcontenttype ("Text/html");//set type to HTML typePrintWriter out =Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("<HEAD><TITLE> please log in and view Notice file </TITLE>"); Out.println ("<style>body,td,div {font-size:12px;} </style> "); Out.println ("</HEAD>"); Out.println ("<BODY>"); Out.println ("<form action=" + request.getrequesturi () + "' method= ' post ' >"); Out.println ("Account: <input type= ' text ' name= ' username ' style= ' width:200px; ' > <br/><br/> "); Out.println ("Password: <input type= ' password ' name= ' password ' style= ' width:200px; ' > <br/><br/> "); Out.println ("<input type= ' submit ' value= ' login ' >"); Out.println ("</form>"); Out.println ("</BODY>"); Out.println ("</HTML>");        Out.flush ();    Out.close (); }         Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {String username= Request.getparameter ("username");//Usernmae parameters for submissionString Password = request.getparameter ("password");//Password parameters for submissionenumeration<string> params = This. Getinitparameternames ();//all initialization parameter names         while(Params.hasmoreelements ()) {String Usernameparam= (String) params.nextelement ();//parameter name, which is the user nameString Passnameparam = Getinitparameter (Usernameparam);//parameter value, which is the password                        if(Usernameparam.equals (username) && passnameparam.equals (password)) {//Displays if the user name and password are matchedRequest.getrequestdispatcher ("/web-inf/notice.html"). Forward (Request,response); return; }        }         This. doget (Request, response);//username, password mismatch, show login page    }}

Web. xml File Related content:

<servlet>    <Servlet-name>Initparamservlet</Servlet-name>    <Servlet-class>Com.helloxr.servlet.InitParamServlet</Servlet-class>    <Init-param>        <Param-name>Helloxr</Param-name>        <Param-value>Password</Param-value>    </Init-param>    <Init-param>        <Param-name>Admin</Param-name>        <Param-value>Admin</Param-value>    </Init-param>    <Init-param>        <Param-name>Babyface</Param-name>        <Param-value>Babyface</Param-value>    </Init-param></servlet>
< servlet-mapping >    < Servlet-name > Initparamservlet</servlet-name>    <Url-pattern >/servlet/initparamservlet</url-pattern>  </  servlet-mapping>

Java Web Learning Note 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.