< lightweight Java EE Enterprise Application Combat >1:jsp/servlet and related technical details (II.)

Source: Internet
Author: User

7 Servlet Introduction

Development of the 7.1 Servlet

@WebServlet (name = "HelloServlet", Urlpatterns = {"/helloservlet")}) Public classHelloServletextendsHttpServlet {@Overrideprotected voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException {req.setcharacterencoding ("UTF-8"); Resp.setcharacterencoding ("UTF-8"); String username= Req.getparameter ("username"); PrintStream out=NewPrintStream (Resp.getoutputstream ());    OUT.PRINTLN (username); }}

After you start Tomcat, use the link http://localhost/helloServlet?username=admin.

Configuration of the 7.2 Servlet

Starting with Servlet 3.0, there are two ways to configure a servlet: @WebServlet annotations or Web. XML configuration.

The life cycle of the 7.3 Servlet

7.4 Load-on-startup Servlet

There are two opportunities to create a Servlet: When the app starts or when the user requests it. The Load-on-startup parameter configures the Servlet instantiation priority, and the smaller the value, the higher the priority.

7.5 Configuration parameters for access Servlet

There are two ways to configure parameters for a Servlet: The InitParams property of the @WebServlet or the <init-param> child element of the <servlet> element in Web. Xml.

@WebServlet (name = "HelloServlet", Urlpatterns = {"/helloservlet")}, InitParams={@WebInitParam (name= "Password", value = "admin123")        }) Public classHelloServletextendsHttpServlet {@Overrideprotected voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException {req.setcharacterencoding ("UTF-8"); Resp.setcharacterencoding ("UTF-8"); PrintStream out=NewPrintStream (Resp.getoutputstream ()); ServletConfig Config=Getservletconfig (); Out.println (Config.getinitparameter ("Password")); }}

7.6 Using a Servlet as a controller

In the standard MVC pattern, JSP is responsible for collecting the user request parameters and presenting the data only as the presentation layer technology, the Servlet is used only as the controller, all requests are sent to the Servlet,servlet call Model processing request, and the JSP renders the processing result.

<%@ Page ContentType="Text/html;charset=utf-8"language="Java" %><HTML><Head>    <title>User Login</title></Head><Body><span>    <%        Stringmessage= (String) Request.getattribute ("message"); if(Message! )= NULL) {out.println (message); }    %></span>Please enter your user name and password:<BR><formID= "Login"Method= "POST"Action= "/loginservlet">User name:<inputtype= "text"name= "username"><BR>Password:<inputtype= "Password"name= "Password"><BR>    <inputtype= "Submit"value= "Login"></form></Body></HTML>
login.jsp
@WebServlet (name = "Loginservlet", Urlpatterns = {"/loginservlet")}) Public classLoginservletextendsHttpServlet {@Overrideprotected voidService (HttpServletRequest req, HttpServletResponse resp)throwsservletexception, IOException {req.setcharacterencoding ("UTF-8"); String username= Req.getparameter ("username"); String Password= Req.getparameter ("Password"); String message= ""; Dbutil Dbutil=NewDbutil (); Try{ResultSet ResultSet= Dbutil.query ("Select password from t_user where username =?", username); if(Resultset.next ()) {if(Resultset.getstring ("Password"). Equals (password)) {//Save username to sessionHttpSession session =req.getsession (); Session.setattribute ("Username", username); //forward to welcome pageReq.getrequestdispatcher ("/welcome.jsp"). Forward (req, resp); } Else{message= "Bad password"; }            } Else{message= "User name does not exist"; }        } Catch(Exception e) {e.printstacktrace (); }        if(!message.equals ("") {Req.setattribute ("Message", message); Req.getrequestdispatcher ("/login.jsp"). Forward (req, resp); }    }}
Loginservlet.java

8 Automatic labeling of JSP 2

< lightweight Java EE Enterprise Application Combat >1:jsp/servlet and related technical details (II.)

Related Article

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.