Servlet Usage Analysis for JSP Learning

  • 2021-06-28 09:39:16
  • OfStack

This paper describes Servlet usage learned by JSP.Share it for your reference.Specific analysis is as follows:

Servlet is an Java program written using the JavaServlet application design interface. Originating from the request/response mode, it can accept Http requests from client browsers, generate a response and return to the client.

Differences and Connections between Applet JSP JavaBean and Servlet

Neither Applet nor Servlet has an main () method. Only some specific methods are used to start execution and exit, but Servlet does not provide a user interface and runs on the server side, while Applet provides a user interface and runs on the client side.
Servlet and JavaBean are both written in Java, but JavaBean cannot run independently, it only provides an interface for JSP to access, while Servlet can run independently.

Before the advent of JSP, Sun introduced Servlet, but because Servlet is used to write HTML scripts, it is necessary to use the print or println method to print the output sentence by sentence, which brings a lot of troubles to development.The JSP web page is an Java code embedded in the HTML script, radically changing the way you program it

JSP JavaBean and Servlet can communicate, for example, JSP can call JavaBean or Servlet, which can be displayed on the JSP page after Servlet processes data.
All Servlets implement the javax.servlet.Servlet interface directly or indirectly

Life cycle of Servlet:

Initializing the call to the init() method
Execute when calling the service() method
End, when the destroy() method is called

Writing method for web.xml:

Line 1 < ?xml version="1.0" encoding="ISO-8859-1"? > Describes the version and character set of xml
Line 2 < web-app > .............. < /web-app > This is the main message for xml

Name and customize URL for Servlet in xml


<web-app 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/web-app_2_4.xsd"
 version="2.4">
<display-name>Servlet technology </display-name>
<description>
 Servlet Example 
</description>
<servlet>
 <servlet-name>SampleServlet1</servlet-name>
 <servlet-class>ch7.SampleServlet1</servlet-class>
</servlet>
<servlet-mappint>
 <servlet-name>SampleServlet1</servlet-name>
 <url-pattern>/ch7/SampleServlet1</url-pattern>
</servlet-mappint>
</web-app>

Then enter: http://localhost:8080/myapp/SampleServlet1 in the browser

I hope that the description in this paper will be helpful to everyone's JSP program design.


Related articles: