SoFunction
Updated on 2025-03-08

Detailed explanation of the difference between jsp and method

getParameter is used to accept parameters passed by post get methods.
getAttribute must first setAttribute.

(1) () Get is to obtain data passed in through the implementation of the container through the implementation of the container. () and getAttribute() are only flowing inside the web container, and are only in the request processing stage.

(2) The data passed by the () method will be transmitted from the Web client to the Web server, representing HTTP request data. The () method returns String type data.

The data passed by the () and getAttribute() methods will only exist inside the web container.

Another point is that the HttpServletRequest class has a setAttribute() method, but does not have a setParameter() method.

Take an example. If there is a link relationship between two WEB pages, that is, when you want to link to, the request parameters can be obtained through the getParameter() method.

If there is

Html code

<form name="form1" method="post" action="">  
Please enter the user name:<input type="text" name="username">  
<input type="submit" name="Submit" value="submit">  
</form>  

If you want to obtain the request parameter username through ("username") method:

Html code

< % String username=("username"); %>  

However, if the forwarding relationship between two WEBs is a forwarding relationship, the forwarding target WEB can use the getAttribute() method to share data within the request scope with the forwarding source WEB. Let's talk about an example.

Have and

I hope to pass the current user name to the user. How to pass this data? First call the following setAttribute() method in:

Html code

<%  
String username=("username");  
("username",username);  
%>  
  
<jsp:forward page="" />  

Get the user name through the getAttribute() method:

Html code

<% String username=(String)("username"); %>  
  1. The HttpServletRequest class has a setAttribute() method, but no setParameter() method
  2. When there is a link relationship between two web components, the linked component obtains the request parameters through the getParameter() method.
  3. When there is a forwarding relationship between two web components, the forwarding target component uses the getAttribute() method to share data within the scope of request with the forwarding source component.

Generally, getParameter is used for parameters passed through forms and links.

Use of assignment through ("name","jerry")

This problem is mainly the difference between request and session. The scope of request is smaller, just a request. Simply put, it is an operation on the page. () is to obtain parameters from the url and form in the previous page. However, if a request involves multiple classes, the parameters must be taken later. You can use () and (), but after the result is output, the request ends.

The session can span many pages, and it can be understood that it is multiple requests issued by the client in the same IE window. Parameters can be passed between them, for example, many website users use it for logging in.

Generally, you can use getParameter to get page parameters. . . String. . .

getAttribute() can get the object. . .

getParameter can get the parameters passed from the page such as? id=123 or something.

getAttribute() is often used to pass parameters to jsp on servlet page

This is the end of this article about the detailed explanation of the differences between jsp () and () methods. For more information about the differences between jsp () and () methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!