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

How to use JavaWeb session Technology

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

Share

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

Editor to share with you how to use JavaWeb conversation technology, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. What is a conversation:

two。 What are the conversation technologies:

What is Cookie?

Cookie is sometimes used in its plural form Cookies. The type is "small text file", which is the data (usually encrypted) stored on the user's local terminal by some websites for Session tracking in order to identify the user's identity, which is temporarily or permanently saved by the user's client computer.

3.cookie Learning case:

Cookie: is an array, with each element having a name and a value

The element corresponding to the name can be found by name.

Important:

Cookie [] cookies = req.getCookies (); / / create the cookie object, get all the cookie array Cookie cookie = CookieUtils.findCookie (cookies, "lasttime") of the browser; / / find the corresponding cookies in the array through: cookies: a name "lasttime", and return the cookie: value

String value = cookie.getValue ()

Resp.getWriter () .println (value); / / value: get the value of cookie (this)

Print on top of the browser

Cookie c = new Cookie ("lasttime", wer); / / create a cookie with the name lasttime and the value resp.addCookie (c) corresponding to the wer variable; / / add to the browser: increase the cookie value of this name, you can

Create: HelloServlet4 extends HttpServlet this class

Package com.example.demo16;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.net.URLEncoder;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;public class HelloServlet4 extends HttpServlet {@ Override public void init () throws ServletException {super.init () This.getServletContext (). SetAttribute ("name", "Zhang San");} @ Override protected void doGet (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {/ / super.doGet (req, resp); Cookie [] cookies = req.getCookies (); Cookie cookie = CookieUtils.findCookie (cookies, "lasttime"); System.out.println (cookie) If (cookie==null) {resp.setCharacterEncoding ("utf-8"); resp.setContentType ("text/html;charset=UTF-8"); resp.getWriter () .println ("Hello, welcome to this website!") ;} else {resp.setCharacterEncoding ("utf-8"); resp.setContentType ("text/htm/UTF-8"); resp.getWriter () .println ("when was your last visit:"); / / Chinese error cookie = CookieUtils.findCookie (cookies, "lasttime"); String value = cookie.getValue (); resp.getWriter () .println (value) Date d = new Date (); / / changing SimpleDateFormat sc=new SimpleDateFormat ("yyyy-MM-dd-HH:mm:ss"); String wer=sc.format (d); / / string System.out.println (wer); / / actually setting: time, time value is java, changing time / / Cookie c=new Cookie ("lasttime", "11111") This can show / / Cookie c = new Cookie ("lasttime", "1-1-1-1-1-1"); / / Cookie c = new Cookie ("lasttime", "2021-11-14"); / / 2021-11-14 cannot be reflected at 20:14, nor can Chinese / / Cookie c = new Cookie ("lasttime", "202111-11-14"). / / No blanks: Cookie c = new Cookie ("lasttime", wer); resp.addCookie (c);} / * resp.getWriter (). Println ("helloworld"); resp.setStatus (302); / / resp.setHeader ("Location", "/ demo16_war/helloo"); resp.setHeader ("Refresh", "5 law url demo 16 charger helloo") * / / String name= (String) this.getServletContext () .getAttribute ("name"); / / System.out.println (name); / / text (); text ();} protected void text () throws IOException {/ / Properties properties=new Properties (); / / create file object / / InputStream is=this.getServletContext () .getResourceAsStream ("WEB-INF/dp.properties") / / the path here, / / properties.load (is); / / String driverClassName=properties.getProperty ("driverClassName"); / / String url=properties.getProperty ("url"); / / String password=properties.getProperty ("password"); / / String usernane=properties.getProperty ("username"); / / System.out.println (driverClassName); / / System.out.println (url); / / System.out.println (password); / / System.out.println (usernane) SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); try {Date d = sdf.parse (toString ()); System.out.println (d);} catch (ParseException e) {e.printStackTrace ();} @ Override protected void doPost (HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {super.doPost (req, resp);}}

Create the CookieUtils class:

Package com.example.demo16;import javax.servlet.http.Cookie;public class CookieUtils {public static Cookie findCookie (Cookie [] cookies, String name) {if (cookies==null) {return null;} else {for (Cookie cookie:cookies) {if (name.equals (cookie.getName () {/ / find the equivalent name name return cookie;}} return null;}

Effect picture:

Click to refresh the page:

4. Note the problems that arise when using cookie:

Cookie c = new Cookie ("lasttime", "2021 11 14"); / / spaces are not allowed

Return: garbled

If the cookie value is Chinese, set it:

Resp.setCharacterEncoding ("utf-8"); response is set to: utf-8 to solve Chinese garbled

The following:

String werr= "who am I"; Cookie c = new Cookie ("lasttime", werr); resp.addCookie (c)

And:

You can put the html tag in resp.getWriter (). Println:

Need

Resp.setContentType ("text/html;charset=UTF-8"); / / to parse

Resp.setContentType ("text/html;charset=UTF-8"); resp.getWriter (). Println ("when was your last visit:"); these are all the contents of the article "how to use JavaWeb conversation Technology". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

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

12
Report