Servlet Learner01, servletlearner01

Source: Internet
Author: User

Servlet Learner01, servletlearner01
Simple mvc creation and running of a servlet

The development environment and deployment environment are different.
This is deployment
Under the tomcat webapps directory

\ Webapps \ Beer-v1 \ form.html

1 <body> 2 

\ Webapplications \ Beer-v1 \ WEB-INF \ web. xml

 1 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 2     <servlet> 3         <servlet-name>Ch3 Beer</servlet-name> 4         <servlet-class>com.example.web.BeerSelect</servlet-class> 5     </servlet> 6  7     <servlet-mapping> 8         <servlet-name>Ch3 Beer</servlet-name> 9         <url-pattern>/SelectBeer.do</url-pattern>10     </servlet-mapping>11 </web-app>

The user entered the form and clicked the submit browser to generate the following request URL/Beer-v1/SelectBeer. do

The container searches for the DD (deployment description file) and finds a <servlet-mapping> that matches/SelectBeer. do.

The <servlet-name> corresponding to <url-pattern> in the container is Ch3 Beer (this is not the name of the actual servlet class file
Ch3 Beer is the servlet name rather than the servlet class name)

For containers, servlet is just a thing named after <servlet> In DD. servlet name is only used in DD for other parts of DD.
Create servlet ing

Container search <servlet-name> is the <servlet> flag of Ch3 Beer

The <servlet-class> container in the <servlet> tag knows which servlet class is responsible for processing this request.
Classes are loaded and initialized before initialization.

The container starts a new thread to process this request and passes the request to the thread (the service () method passed to the servlet)
Container response is sent back to the customer through the WEB Server

Servlet is a controller

\ Webapps \ Beer-v1 \ WEB-INF \ classes \ com \ example \ web \ BeerSelect. class

1 package com. example. web; 2 import javax. servlet. requestDispatcher; 3 import javax. servlet. servletException; 4 import javax. servlet. http. httpServlet; 5 import javax. servlet. http. httpServletRequest; 6 import javax. servlet. http. httpServletResponse; 7 import java. io. IOException; 8 import java. io. printWriter; 9 import java. util. iterator; 10 import java. util. list; 11 /////////////////////////////////////// //////////////////////////////////////// /// // 12 import com. example. model. *; 13 14/** 15 * Created by Melody on 2015/10/29. 16 */17 public class BeerSelect extends HttpServlet18 {19 public void doPost (HttpServletRequest request, 20 HttpServletResponse response) 21 throws IOException, servletException22 {23 // This method comes from the ServletResponse interface 24 // 02response. setContentType ("text/html"); 25 // 02 PrintWriter out = response. getWriter (); 26 // 02out. print ("Beer Selection Advice <br/> "); 27 // This method comes from the ServletResponse interface. Note that this parameter must match the name attribute in the HTML <select> tag with 28 String c = request. getParameter ("color"); // The color is <select name = "color"> 29 // String [] cArrays = request. getParameters ("color"); this accepts an array (one parameter has multiple values) 30 // 01 out. print ("<br/> Got beet color" + c); 31 32 BeerExpert be = new BeerExpert (); 33 List result = be. getBrands (c); 34 35 // Add an attribute to the request object for jsp use. Note that JSP should look for style36 request. setAttribute ("style", "result"); 37 38 // instantiate a request dispatcher for JSP 39 RequestDispatcher view = request. getRequestDispatcher ("result. jsp "); 40 41 // use the request distributor to require the container to prepare JSP and send requests and responses to JSP 42 view. forward (request, response); 43 44 // 02 Iterator it = result. iterator (); 45 // 02 while (it. hasNext () 46 // 02 {47 // 02 out. print ("<br> try:" + it. next (); 48 // 02} 49} 50}

\ Webapps \ Beer-v1 \ WEB-INF \ classes \ com \ example \ model \ BeerExpert. class

Model Layer

1 package com. example. model; 2 import java. util. *; 3 // you can see that no framework is needed. This is a very common java class 4 public class BeerExpert 5 {6 public List getBrands (String color) 7 {8 List brands = new ArrayList <> (); 9 if (color. equals ("amber") 10 {11 brands. add ("Jack Amber"); 12 brands. add ("Red Moose"); 13} 14 else15 {16 brands. add ("Jail Pale Ale"); 17 brands. add ("Gout Stout"); 18} 19 return brands; 20} 21}

\ Webapplications \ Beer-v1 \ result. jsp

This is the view layer.

<%@ page import = "java.util.*" %>

Modeling and Testing
A model means that the application background cannot restrict the model to only one web application. Therefore, it should have its own toolkit.
The models do not include tomcat class models. Just like an Expert expert returns an answer to the servlet. This answer is added to the request object.

Servlet forwards the request object to JSP. Obtain the answer from the request object. JSP generates a page for the container. The Container returns this page.
Add the model answer to the request object for JSP access.
Send the container request to "result. jsp"

Model requirements
The package must be com. example. model.
Its directory structure should be/WEB-INF/classes/com/example/model
Get

 

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.