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

Example Analysis of filling in blanks with Java Web keyword

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points of Java Web keyword fill in the blanks example analysis, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

(1) in TestServletRequest, the information named "param" with the value of "HelloWorld" is stored in the request range, and the request is forwarded to another Servlet--AnotherServlet. In AnotherServlet, read the value of param from within the range of request. Please complete the following Servlet program.

File name: TestServletRequest.java@WebServlet ("/ TestServletRequest") public class TestServletRequest extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request._1_ ("param", "HelloWorld"); RequestDispatcher rd=request.____2_ ("_ 3 _"); rd._4_ (request,response) }} File name: AnotherServlet.java@WebServlet ("/ AnotherServlet") public class AnotherServlet extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String param= (String) request.____5_ ("param");}}

1. SetAttribute

2. GetRequestDispatcher

3. / AnotherServlet

4. Forward

5. GetAttribute

(2) in the myweb application, a Servlet named HelloWorld is created, in which the request parameter values are read and output. Please complete the following Servlet program.

Public class HelloWorld extends _ 1encrypted requests _ {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / A pair of requests submitted by users are decoded with utf-8, otherwise garbled request.____2____ ("utf-8") will occur; / / by setting Content-Type (content type), tell the browser that the next message is HTML and the encoding method is UFT-8 Response.____3____ ("text/html;charset=UTF-8"); / / get the value String name = request.____4_ ("name") of the name parameter in the request object; String greeting = "Hello" + name + "!"; / / get the output stream object PrintWriter out = response.____5___ (); out.println (greeting);}}

1. HttpServlet

2. SetCharacterEncoding

3. SetContentType

4. GetParameter

5. GetWriter

(3) in the myweb application, enter the user name and password on the form page, and use the "post" method to submit the form data to the Servlet program. The mapping url of Servlet is configured as: / Process. Determine whether the entered user is "admin" in Servlet, and if so, redirect to another Servlet (Admin.java) whose mapping url is "/ Admin".

Please complete the following Servlet program.

Mapping urlpublic class Process extends HttpServlet {public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException of Servlet, IOException {/ / get request parameter value String un = request._2_ ("username") If (unloaded null) {if (un._3____ ("admin")) {response._4_ ("_ 5");}

1. @ WebServlet ("/ Precess")

2. GetParameter

3. Equals

4. SendRedirect

5. Admin

(4) in the myweb application, create a Servlet named Servlet1, and its mapping url is: / Servlet1. Enter an address in the browser address bar to access the Servlet, and enter the address as follows:

Http://localhost:8080/myweb/Servlet1?param1=111

Take the param1 parameter in this Servlet, pass it to another Servlet--Servlet2, and jump to Servlet2 by forwarding the request.

Please complete the following Servlet1 program.

Public class Servlet1 extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get the value of the request parameter named param1 corresponding to String param1= request.____1_ ("_ _ 2 request parameters _"); / / save to request._3_ ("param1", param1) in the range of request / / get the forwarder object RequestDispatcher rd=request._4____ ("/ Servlet2"); / / Jump to Servlet2 rd.___5____ (request,response) using forwarding request;}}

1. GetParament

2. Paraml

3. SetAttribute

4. GetRequestDispatcher

5. Forward

(5) filter

@ WebFilter (/ / filter the "/ WebSite" URL format only urlPatterns = {"_ _ 1encrypted formats _"}, initParams = {@ WebInitParam (name = "site", value = "dalian")}) public class MyFilter implements Filter {protected String site Public void destroy () {} public void _ 2 _ (ServletRequest req, ServletResponse res, FilterChain fchain) throws IOException, ServletException {/ / if site is not empty, output site if (_ 3 _) {System.out.println ("website" + site);} _ _ 4___.doFilter (req, res) } public void init (FilterConfig conf) throws ServletException {/ / get the initialization parameter this.site= _ _ 5___.getInitParameter ("site") configured in the @ WebFilter annotation;}}

1. / Website

2. DoFilter

3. Sitebasket null

4. Fchain

5. Conf

(6) in the myweb application, a Servlet named Servlet1,Servlet1 is created, a property named "name" is stored in the session scope, and then redirected to Servlet2. Get the value of the "name" attribute in the session scope in Servlet2 and output it. Please complete the following Servlet2 program.

Public class Servlet2 extends HttpServlet {public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/ / get the current session object HttpSession session=request._1_ (); / / get the attribute value String name= (String) session._2_ ("_ 3 _") in the session object / / get the output stream object PrintWriter out=response._4_ (); / / output name out._5_ (name);}}

1. GetSession

2. GetAttribute

3. Name

4. GetWriter

5. Print

These are all the contents of the article "sample Analysis of filling in the blanks with Java Web keywords". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.

Share To

Development

Wechat

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

12
Report