• 周六. 10 月 5th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Brief introduction of Java Web Development Based on Servlet

King Wang

1 月 3, 2022

Because in the process of learning , Touch this part of the content , So it’s specially organized here , Share with you .

1、 Definition

Servlet yes Java Servlet For short , This is called a small service program or service connector ;

2、 principle

In principle ,Servlet Can respond to any type of request , But most of the time Servlet Only to extend the base HTTP Agreed Web The server .Servlet yes Java

Servlet API A kind of development Java class , these API Be included in javax.Servlet and javax.Servlet.http In these two bags .Servlet The program runs on the server side , Generate… Dynamically Web page .

                                     

3、 advantage

With the traditional CGI Like many others CGI Compared with ,Java Servlet With higher efficiency , Easier to use , Better portability , More efficient .

4、JSP And Servlet The relationship between

so to speak JSP The implementation is based on Servlet Of , When JSP The container is connected to a JSP After the page request , First of all, I will judge with JSP The document corresponds to Servlet The name of the class

① If it doesn’t exist or is old , Then the container will recreate an equivalent Servlet Class and compile .

② If it exists , Then the server compiles Servlet class , And automatically load to form Servlet example , And return the execution results to the client .

5、Servlet Life cycle of

                                 

 

public void init() throws ServletException {    }

// initialization ,init Method is called only once . It was created for the first time Servlet When called , It is not called at every subsequent user request . therefore , It’s for disposable use initialization .

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }

// Yes Get Method request response service ,GET The request came from a URL Normal request for , Or from an unspecified METHOD Of HTML Forms , It consists of doGet() Method treatment .

③    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {    }

// Yes Post Method request response service ,POST The request came from a specially designated METHOD by POST Of HTML Forms , It consists of doPost() Method treatment .

public void destroy() {    }

// The destruction ,destroy() Methods are called only once , stay Servlet Called at the end of the lifecycle .destroy() Methods can make your Servlet Close database connection 、 Stop backstage Threads .

 

6、Web.xml To configure

One Tomcat The most important configuration file in the project .

web.xml No, actually it can be —————— Just make sure you don’t need any filters in your project 、 Monitor 、Servlet wait .

<web-app> //web-app Define the root element of the document . 
<servlet>
<servlet-name>firstServlet</servlet-name>
// Used to define servlet The name of , The name must be unique throughout the application .
<servlet-class>servlet.HelloServlet</servlet-class>
// Used to specify servlet The fully qualified name of .
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
//Servlet Name , Uniqueness and consistency , And <servlet> The names declared in the element are the same .
<url-pattern>/hello</url-pattern>
// Specify relative to Servlet Of URL The path of . The path is relative to web The root path of the application context .
//<servlet-mapping> take URL A pattern maps to something Servlet, That is, the Servlet To deal with the URL.
</servlet-mapping>
</web-app>

7、Servlet Main purpose of —— Handling customer requests and responses

HttpServletRequest Interface ————-javax.servlet.http.HttpServletRequest The object corresponding to the interface type JSP Medium request Built-in objects .

Common methods :


public String getParameter(String param);
// Get client request parameters , Gets the parameter value of the specified name
public String[] getParameterValues(String param);
// Gets an array of all values for the specified name parameter . It is applicable to the case that a parameter name corresponds to multiple values . Such as the check box in the page form , Values submitted by multiple selection list .
public void setAttribute(String attname,Object o);
// stay JSP Built-in objects session and request There's a way , The purpose of this method is to save data , And then you can use getAttribute Method to take out .
public Object getAttribute(String attname);
// Take the data

 

HttpResponse Interface ———javax.servlet.http.HttpResponse The object corresponding to the interface type JSP Medium response Built-in objects .

Common methods :

public void setContentType(String contentType);
// The content format and length can be indicated in the response .
public void sendRedirect(String url);
// Send a temporary redirection response to the client , Let the client access the new URL.
// If the specified position is relative URL,Servlet Before the container sends the response to the client , Must be relative to URL Convert to absolute URL.
/ If the response has been submitted , This method will throw IllegalStateException abnormal .

ServletConfig Interface ————–javax.servlet.ServletConfig The object corresponding to the interface type JSP Medium config Built-in objects , Used in Servlet Initialization orientation Servlet Pass on some information .

Definition :ServletConfig config = this.getServletConfig();

Common methods

public String getInitParameter(String paraname);
// from web.xml Get parameters in .

ServletContext Interface ————-javax.servlet.ServletContext The object corresponding to the interface type JSP Medium application Built-in objects .

Definition :ServletContext app = this.getServletContext();

Common methods

public void setAttribute(String attname, Object o);
// Storing data , for example :setAttribute("wg","666"),
// Indicates that a property named wg, The property value is 123 The data of ,
// If you call the method again , And put the same properties in the container , The last saved will cover the previous one .
public Object getAttribute(String attname);
// Take out the data of the specified attribute name , Include property name and attribute value ,
// Yes name The specified data does not exist , Then this method does nothing .

 

About Servlet For the time being, I’d like to introduce you here !

Thank you. !

发表回复