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

The method of embedding java Code in jsp

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the knowledge of "how to embed java code in jsp". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

A brief introduction to jsp

The emergence of jsp is to solve the problem of splicing a large number of html codes in Serlvet. At the same time, html,js,css and java codes can be written directly in jsp technology.

Embed java code in jsp

You can write java code in jsp, but you need to use the appropriate tags to encapsulate java code. There are three ways:

The jsp script declares that you can write member variables, methods, and inner classes

Jsp script expression, which is used to output data directly

Script snippet of jsp, which can be mixed with java code and html code input

EL expression

EL's full name is Expression Language, and its function is mainly to extract data from different ranges on jsp pages, which are ServletContext ranges and Request. In servlet, servletContext represents the current web project, and each project has a unique ServletContext and its corresponding, so the data placed in the servletContxt page can be extracted through the el expression, and each request represents a request object, so the data stored in the request object can also be extracted using the el expression on the jsp page. In the form of * * ${key} * *

Introduction to cookiecookie

Cookie itself is still an object inside the server, but this object will eventually be given to the client object in the response, and the client (usually the browser) will recognize the cookie information, and the browser will carry the cookie in the request header and send it to the server during subsequent access.

The browser carries the characteristics of cookie, which is specified by W3C's http protocol and is carried in the request header. If the response data contains cookie,cookie, it is also in the response header (note that it is not in the response body).

From the connection between the client and the server above, we can know that cookie belongs to the conversation technology.

What is conversation?

When the user opens the browser to access the server, in a series of access process, these operations of the user are called the dialogue between the user and the server (the conversation between the client and the server). In the process of interaction, a series of operations of the user will produce some data that needs to be saved, so it is necessary to use cookie or session (next introduction) technology.

The server setting cookie of the small case returns to the client servletpackage top.cookies;import javax.servlet.http.Cookie;import java.io.IOException;public class cookieServlet extends javax.servlet.http.HttpServlet {protected void doGet (javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {Cookie cookie = new Cookie ("name", "testCookie"); / / response.addCookie (cookie); response.getWriter () .write ("helloworld") }} xml configure Servlet top.cookies.cookieServlet Servlet / cookie to request the result for the first time (no cookie is set when returned)

The first thing here is to comment out the response.addCookie (cookie); that is, there is no cookie in the returned data, and the result is as follows:

First request (set cookie when returned)

Now release the comment line, and the request result is as follows:

You can see that the cookie information is already in the response, but there is no cookie information in the request header.

Second request

Since cookie is set in the response on the first request, the cookie information should be carried in the request header on the second request. The result is as follows:

You can see that the cookie information is contained in the request header.

Get cookie

If the cookie is set by the server and given to the client, then getting the cookie is actually getting the cookie from the request header, that is, getting the cookie from the httpServletRequest object. Since the user can carry multiple cookie, you can use the getCookies () method to get the cookie array.

Cookie [] cookies = request.getCookies (); for (Cookie cookie: cookies) {System.out.println (cookie.getName () + "-" + cookie.getValue ());} cookie classification

Cookie is mainly divided into two categories:

Cookie at the temporary session level

If the cookie sent by the server to the client does not specify the save time of the cookie on the client, the cookie will only exist during the running of the browser. When the browser is closed, the cookie in the client will disappear.

Persistent cookie

When the server sends the cookie to the client, you can specify how long the cookie will survive on the client. This can be done through the setMaxAge (int) method.

The unit of this method is seconds.

If the number is negative, the cookie is not stored.

If 0 means to delete the cookie

Set the path for cookie

When setting cookie, each cookie object needs to be consistent with the face-to-face access path. If the set cookie and path are inconsistent, the cookie information cannot be obtained on the server. You can use the setPath (String) method in the cookie object to set the path, which generally sets the project root directory. In this way, as long as the content in the current project is accessed, the request header will carry the corresponding information. For example, if the response path is not set when setting cookie in UEFA Champions League, the default path will be used.

Set the role of the access path

The function of setting each cookie's own access path is to determine whether it is necessary to carry the current cookie data when visiting each website, because there may be several cookie objects with different functions in the background of a website, but these objects do not need to be brought to the server for every request, so different paths can be set for different cookie objects when they are generated. In this way, when the client accesses the server, it can carry different cookie according to different access paths.

This is the end of the content of "how to embed java code in jsp". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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