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 are the knowledge points of Servlet and Jsp

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article will explain in detail what are the knowledge points about Servlet and Jsp, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Create a new Servlet

Override the service method, which takes two parameters: req and resp, which are called request and response in jsp.

Encoding setting resp.setContentType ("text/html;charset=utf-8"); req.setCharacterEncoding ("utf-8"); creating Service layer object UserService us = new UserServiceImpl (); processing request

If a servlet needs to process multiple requests, then servlet needs to identify which request is coming, and you can add an input tag to the form form of jsp

The value of the type attribute is hidden, which does not affect the effect of the page. Set the name attribute of all the tags that distinguish requests to the same value, here called 'oper',value, to different values.

Make a judgment in the service method and call different methods respectively

/ / get operation type String oper = req.getParameter ("oper"); if ("login" .equals (oper)) {/ / call login verification method CheckUserLogin (req,resp) } else if ("reg" .equals (oper)) {/ / call registration function userReg (req,resp) } else if ("out" .equals (oper)) {/ / call exit function userOut (req,resp) } else if ("pwd" .equals (oper)) {/ / call password modification function userChangePwd (req,resp) } else if ("showAll" .equals (oper)) {/ / call the function to view all user information userShowAll (req,resp);} else {System.out.println ("corresponding action not found" + oper) }

The req.getParameter (property name) method gets the data submitted in the form form.

So how does the form form know which servlet to submit for processing?

You can set the value of the action attribute to the alias of servlet by setting the action attribute of the form tag.

So if there is no form form, for example, I just click a button on the page and want servlet to handle it, how do I achieve it? Do the following:

Function tuichu () {var flag = window.confirm ("exit?") ; if (flag) {/ / submit oper [xss_clean] ("); [xss_clean] ("); [xss_clean] ("); document.form1.submit ();}} request forwarding and redirection using form form

There are two ways to realize page jump in servlet: request forwarding and redirection.

Request forwarding:

Req.getRequestDispatcher ("login.jsp") .forward (req, resp)

Request forwarding is still a request, the url in the browser's address bar will not change, and the data in the req will still be there.

Redirect:

Resp.sendRedirect ("main.jsp")

Redirects are two requests, the url changes, and the data in the req is gone.

Cookie and Session

How to complete the data flow for different requests?

It is impossible to achieve without the data in req.

There are two ways: cookie and session

Cookie: store some data in the browser, the amount of data is small.

Basic usage:

/ / create cookie Cookie c = new Cookie ("uname", "zhangsan"); Cookie c2 = new Cookie ("color", "red"); / / set the validity period of cookie in seconds c2.setMaxAge (3600); / / set path c2.setPath ("/ Servlet03/gc"); / / add to browser resp.addCookie (c); / / temporarily store resp.addCookie (c2) / / through the validity period set above, the fixed length storage of cookie record for one hour is realized / / cookie Cookie [] cks = req.getCookies (); if (cksrecording null) {for (Cookie c:cks) {System.out.println (c.getName () + ":" + c.getValue () }}

Session: store the data in the browser and store an id of session in cookie to connect to the session object on the server side

Basic usage:

String name = "Zhang San"; / / create session object HttpSession hs = req.getSession () / / this line of code both creates and acquires session / / sets session aging hs.setMaxInactiveInterval (5); / / sets the time to 5 seconds hs.invalidate () / / forced invalidation / / Storage data hs.setAttribute ("name", name). This is the end of the knowledge about Servlet and Jsp. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report