[Spring MVC] learning notes-Basic Servlet and mvcservlet

Source: Internet
Author: User

[Spring MVC] learning notes-Basic Servlet and mvcservlet

ServletIs an application written in Java. It runs on the server, processes the request information, and sends it to the client.

The Servlet client initiates a request and obtains the response from the request.

For all client requests, you only need to create the Servlet instance once (this is an important difference with CGI (Common Gateway Interface), CGI is the creation of a new instance for each request ), therefore, it saves a lot of memory.

After initialization, the Servlet will reside in the memory. Therefore, no load is required for each request.

 

The following example shows how to compile a simple Servlet.

 

Preparations:

1. download and start Tomcat (as the Servlet container) instance step: 1. create a new folder and helloapp 2 under Tomcat/webapps. create the helloapp subfolders as follows helloapp ---- WEB-INF -------- web. xml -------- classes -------- lib 3. create a new java file under classes, Hello. java 4. introduce servlet-api.jar, import javax. servlet. *; 5. servlet implementation can be divided into three methods (important) HttpServlet inherits from GenericServlet, and GenericServlet implements Servlet.5.1 implement Servlet Interface
Public class Hello implements Servlet {@ Overridepublic void init (ServletConfig SC) throws ServletException {// The initialization function is only called for the first time. System. out. println ("initialization... ") ;}@ Overridepublic ServletConfig getServletConfig () {return null ;}@ Overridepublic void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {// each access is called, business logic is written here. System. out. println ("call service"); PrintWriter pw = res. getWriter (); pw. write ("My first web app. ") ;}@ Overridepublic String getServletInfo () {return null ;}@ Overridepublic void destroy () {// called when the last close. System. out. println ("destroy ...");}}

 

5.2 inherit from GenericServlet

 

Public class Hello2 extends GenericServlet {@ Overridepublic void service (ServletRequest sr, ServletResponse sr1) throws ServletException, IOException {// business logic, each access is called. System. out. println ("Testing..."); PrintWriter pw = sr1.getWriter (); pw. println ("my second web app .");}}

 

5.3 inherit from HttpServlet

 

public class Hello3 extends HttpServlet{@Overridepublic void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException{System.out.println("do get method...");PrintWriter pw = res.getWriter();pw.println("do get method.");}public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException{System.out.println("do post method...");PrintWriter pw = res.getWriter();pw.println("do post method.");}}

 

6. Compile the source file

7. modify web. xml

<Servlet> <servlet-name> Hello </servlet-name> // servlet name. test. servlet. hello </servlet-class> // corresponding class </servlet> <servlet-mapping> <servlet-name> Hello </servlet-name> // servlet name, the name must be the same as that in servlet. <Url-pattern>/h1 </url-pattern> // url in the browser. </Servlet-mapping>

8. Access helloapp/h1 and you can see the result.


How does spring mvc remove the-servlet after the configuration file?

"I just want to remove the servlet behind the jdbc-servlet.xml", to remove this, you need
<Servlet>
<Servlet-name> jdbc </servlet-name>
<Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class>
<Load-on-startup> 1 </load-on-startup>
</Servlet> Add the namespace parameter of init-param, because the default <servlet-name>-servlet. xml in spring, you have to add parameters.

Why cannot I retrieve the Servlet jump value in SpringMVC?

The form action should be servlet. After the servlet completes processing, it will jump to the result page.
 

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.