Servlet and JSP Learning guidance and practice (i)

Source: Internet
Author: User

Objective:

How does javase span to Java EE? Originally the Java language was only developed for application desktop applets, but it went out of hand since it followed CGI into server-side development. First Servlet1.0, then 2.0, then 3.0 ... After that, various web-based frameworks developed rapidly, spring,struts,struts2 and so on. Many people who work in Java start with JSP, even from the framework, but don't know what the servlet is? This series will be devoted to the ancestor---"servlet" of the Java Web, which tells about JSP-related content, but in fact JSPs are also servlets.

----------------------------------------------------

Part1:servlet Main API Introduction:

To tell the servlet, let's start with the interfaces and classes in the Javax.servlet,javax.servlet.http two package. Take a look at the UML diagram below:

Figure 1

Figure 2

Where the "servlet" interface is an interface that any servlet must implement directly or indirectly! It is common to have init (), service (), Destroy () three methods, there are not very common methods getservletinfo () and Getservletconfig (). In general, service () is our custom servlet is a business operation that focuses on, while the Init () and destroy () one occur when the servlet is initially called, and one that occurs when the servlet is destroyed!

Request,response can be said to be the most common 2 variables in a Web application. By tracing us from the above UML we can know that they originate from the ServletRequest and Servletresponse interfaces. But most of the time we see the request has been wrapped in more than one layer of intermediate class. For example: HttpServletRequest, or map<string,object> request in struts.

As an introductory article, we are not going to delve into the matter for the time being. Here's a small example that lets us familiarize ourselves with the steps required to implement a basic servlet.

    • STEP1:

New "Web Project", must contain Java EE related jar package;

    • STEP2:

Create a new Java class and implement the Servlet interface or inherit the Genericservlet class.

Override the Service () method to print the HTML statement.

    • STEP3:

Deploy by annotation or by deploying the file "Web. xml".

----------------------------------------

Part2: Instance app01a

Way one: Implement the Servlet interface

Code:

 Packageapp01a;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJavax.servlet.Servlet;ImportJavax.servlet.ServletConfig;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRequest;ImportJavax.servlet.ServletResponse;ImportJavax.servlet.annotation.WebServlet;//@WebServlet (name = "Myservlet", Urlpatterns = {"/my", "/your"}) Public classMyservletImplementsServlet {@Override Public voiddestroy () {System.out.println ("--destroy--"); } @Override PublicServletConfig Getservletconfig () {//TODO auto-generated Method Stub        return NULL; } @Override PublicString Getservletinfo () {//TODO auto-generated Method Stub        return NULL; } @Override Public voidInit (ServletConfig arg0)throwsservletexception {//TODO auto-generated Method Stub} @Override Public voidService (ServletRequest request, servletresponse response)throwsservletexception, IOException {printwriter out=Response.getwriter (); Out.print ("); }}

Deployment file: Web. xml

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app version= "2.5"3Xmlns= "Http://java.sun.com/xml/ns/javaee"4Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee6http//java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">7     8<servlet>9<servlet-name>myServlet</servlet-name>Ten<servlet-class>app01a. myservlet</servlet-class> One</servlet> A      -<servlet-mapping> -<servlet-name>myServlet</servlet-name> the<url-pattern>/My</url-pattern> -</servlet-mapping> -</web-app>

Project file layout:

Test results:

Way two: inheriting the Genericservlet class

Code

1  Packageapp01a;2 3 Importjava.io.IOException;4 ImportJava.io.PrintWriter;5 6 ImportJavax.servlet.GenericServlet;7 Importjavax.servlet.ServletException;8 Importjavax.servlet.ServletRequest;9 ImportJavax.servlet.ServletResponse;Ten ImportJavax.servlet.annotation.WebServlet; One  A@WebServlet (name= "Myservletwithgeneric", urlpatterns={"/mygeneric"}) -  Public classMyservletwithgenericextendsGenericservlet { -  the @Override -      Public voidService (ServletRequest request, servletresponse response) -             throwsservletexception, IOException { -  +PrintWriter out =Response.getwriter (); -Out.print (" ++ "</body>); A  at     } -  -}

Test results:

------------------------

Note: The prerequisite for implementing this example is that the tomcat! is already installed. Without the Tomcat environment, http://tomcat.apache.org

Servlet and JSP Learning guidance and practice (i)

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.