In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the JavaEE Servlet core API how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this JavaEE Servlet core API how to use the article will have a harvest, let's take a look.
1. Introduction to core API 1. Servlet execution process
Servlet is one of the three major components of JavaWeb (Servlet, Filter, Listener). It belongs to dynamic resources. The function of Servlet is to process the request, and the server will send the received request to Servlet for processing. In Servlet, it usually needs to receive the request data, process the request, and complete the response.
2. Introduction to core API API function description ServletConfig acquires servlet initialization parameters and servletContext objects. ServletContext shares data among dynamic resources throughout the Web application. ServletRequest encapsulates the Http request information and creates it at the time of the request. ServletResponse encapsulates the Http response information and is created on request. II. ServletConfig interface 1. Brief introduction to the interface
When the container initializes the servlet, it creates a servletConfig object for the servlet, passes it through the init () method, and saves it in the Servlet object. Core role: 1. Get initialization information; 2. Gets the ServletContext object.
2. Code case
Configuration file
My-name cicada servletOneImpl com.node02.servlet.impl.ServletOneImpl servletOneImpl / servletOneImpl
API usage
Public class ServletOneImpl implements Servlet {@ Override public void init (ServletConfig servletConfig) throws ServletException {String servletName= servletConfig.getServletName (); System.out.println ("servletName=" + servletName); String myName= servletConfig.getInitParameter ("my-name"); System.out.println ("myName=" + myName); Enumeration paramNames = servletConfig.getInitParameterNames () While (paramNames.hasMoreElements ()) {String paramKey= String.valueOf (paramNames.nextElement ()); String paramValue= servletConfig.getInitParameter (paramKey); System.out.println ("paramKey=" + paramKey+ "; paramValue=" + paramValue);} ServletContext servletContext = servletConfig.getServletContext (); servletContext.setAttribute ("cicada", "smile");}} III. ServletContext Interface 1, Interface introduction
A project has only one ServletContext object, which can be obtained in multiple Servlet, and can be used to pass data to multiple Servlet. The object is created when the Tomcat is started and destroyed when the Tomcat is closed! The function is to share data among dynamic resources of the entire Web application.
Acquisition mode
1, ServletConfig#getServletContext (); 2, GenericServlet#getServletContext (); 3, HttpSession#getServletContext () 4, ServletContextEvent#getServletContext () 2, four domain objects
ServletContext is one of the four domain objects of JavaWeb:
1 、 PageContext;2 、 ServletRequest;3 、 HttpSession;4 、 ServletContext
All domain objects have the ability to access data because there is a Map inside the domain object to store data.
3. Code case
Configuration file
My-blog 2019-11-19 servletTwoImpl com.node02.servlet.impl.ServletTwoImpl servletTwoImpl / servletTwoImpl
API usage
Public class ServletTwoImpl extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ("text/html;charset=utf-8"); / / 1, parameter passing ServletContext servletContext = this.getServletContext (); String value= String.valueOf (servletContext.getAttribute ("cicada")); System.out.println ("value=" + value) / / 2. Get initialization parameters String myBlog= servletContext.getInitParameter ("my-blog"); System.out.println ("myBlog=" + myBlog); / / 3. Get application information String servletContextName= servletContext.getServletContextName (); System.out.println ("servletContextName=" + servletContextName); / / 4. Get path String pathOne = servletContext.getRealPath ("/") String pathTwo= servletContext.getRealPath ("/ WEB-INF/"); System.out.println ("pathOne=" + pathOne+ "; pathTwo=" + pathTwo); response.getWriter () .print ("execution: doGet; value:" + value);}} IV, ServletRequest interface 1, interface introduction
The HttpServletRequest interface inherits the ServletRequest interface and is used to encapsulate the request information. This object is created and passed into the service () method of the servlet each time the user requests servlet, in which the incoming servletRequest will be forced to be converted into a HttpservletRequest object to process the HTTP request information. Core role: 1. Get the request message information; 2. Get the network connection information; 3. Gets the requested domain attribute information.
2. Code case
Configuration file
ServletThreeImpl com.node02.servlet.impl.ServletThreeImpl servletThreeImpl / servletThreeImpl
API usage
Public class ServletThreeImpl extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / http://localhost:6003/servletThreeImpl?myName=cicada String method= request.getMethod (); System.out.println ("method=" + method); / / GET String requestURL= request.getRequestURL () .toString (); / / http://localhost:6003/servletThreeImpl System.out.println ("requestURL=" + requestURL) String requestURI= request.getRequestURI (); System.out.println ("requestURI=" + requestURI); / / servletThreeImpl String queryString= request.getQueryString (); System.out.println ("queryString=" + queryString); / / myName=cicada String myName= request.getParameter ("myName"); System.out.println ("myName=" + myName); / / cicada}} V, ServletResponse Interface 1, Interface introduction
HttpServletResponse inherits from ServletResponse and encapsulates Http response information. For each request from the client, the server creates a response object and passes it to the Servlet.service () method. Core role: 1. Set the response header information; 2. Send status code; 3. Set the response text; 4. Redirect
2. Code case
Configuration file
ServletFourImpl com.node02.servlet.impl.ServletFourImpl servletFourImpl / servletFourImpl
API usage
Public class ServletFourImpl extends HttpServlet {@ Override protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType ("text/html;charset=utf-8"); response.setCharacterEncoding ("UTF-8"); response.setStatus; response.getWriter (). Print ("Hello, cicada");} 3, forwarding and redirection
Forwarding
Control of page jump on the server side
Request.getRequestDispatcher ("/ forwarding address") .forward (request, response)
Redirect
The server responds to the jump information, and the browser performs the page jump.
Response.sendRedirect ("redirect address")
Comparison of forwarding and redirection
Differentiate forward redirect address bar unchanged jump server jump number of browser jump requests once and twice data will not be lost in the domain
This is the end of the article on "how to use JavaEE's Servlet core API". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use JavaEE's Servlet core API". If you want to learn more, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.