Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of Java Web Servlet?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "what is the role of Java Web Servlet", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what is the role of Java Web Servlet" bar!

A complete Java Web usually consists of presentation layer, control layer, business layer and data access layer. The presentation layer is usually built by HTML and JSP pages, the control layer is usually Servlet, and the business logic layer is JavaBean or EJB. The components of the data access layer are declared in the configuration file (.xml) and packaged to the Java Web server (Tomcat...). Run in the middle.

Java Web Servlet is a server-side program written in java. Because it is a Java class, it has the advantages of portability and extensibility. Get Java support through javax.servlet and javax.servlet.http packages. In particular, the javax.servlet.http package provides methods for the HTTP protocol, including handling HTTP requests and responses (HttpServlet classes), which are handled by doGet () and doPost (), respectively.

Java code

Protected void doGet (HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException; protected void doPost (HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException

Processing of customer requests (HttpServletRequest class)

Java code

HttpServletRequest request; String name = request.getParameter ("username")

Send the request to the customer in HTML form (HttpServletResponse class)

Record user sessions in the current Servlet (HttpSession class)

Java code

HttpServletRequest request; HttpSession session = request.getSession (true)

Submit or delegate the request of Servlet to another resource (RequestDispatcher class). The RequestDispatcher points to the URL of a specific resource, which can be generated by the getRequestDispatcher () method of the HttpServletRequest object.

Java code

RequestDispatcher rd = request.getRequestDispatcher ("WELCOME.JSP"); rd.forward (request,response)

Since Servlet is usually a controller component in Java Web, it needs to be declared in the configuration file (web.xml).

Java code

Web.xml

< servlet > < servlet-name > myServlet < / servler-name > < servlet-class > myServlet < / servlet-class > < / servlet > < servlet-mapping > < servlet-name > myServlet < / servlet-name > < url-pattern > / myServlet < / url-pattern > < / servlet-mapping >. Thank you for your reading, the above is the content of "what is the role of Java Web Servlet", after the study of this article, I believe you have a deeper understanding of the role of Java Web Servlet, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report