request Object JSP Learning

Keywords: Java JSP Attribute xml

Author: u014470581 --- Thank you for the original, thank you for the author!!

1. The request object is an important object in JSP. Each request object encapsulates a user request, and all request parameters are encapsulated in the request object, so the request object is an important way to get request parameters.

2. Another request represents the scope of this request, so it can also be used to manipulate the properties of the request scope.

Get Request Header/Request Parameters

1. web applications are applications of request/response architecture. Browsers usually send requests with some request headers and may contain some request parameters to send to the server. The server is responsible for resolving the request header/request parameters by JSP or Servlet, and the way JSP and Servlet get the request parameters is request.

2. request is an instance of the HttpServletRequest interface, which provides the following methods:

String getParanmeter(String paramName): Gets the value of the paramName request parameter

Map getParameterMap(); Gets a Map object consisting of all request parameter names and values

Enumeration getParameterMap(): Gets the Enumeration object consisting of all request parameter names

String[] getParameterValues (String name); the value of the paramName request parameter, which returns an array of values when the request parameter has more than one value

3. HttpServletRequest provides the following ways to access request headers

String getHeader (String name); based on the value of the specified request header

Java.util.Enumeration<String> getHeaderNames(): Get the values of all request headers

Java.util.Enumeration<String> getHeaders (String name) obtains multiple values for the specified request header

int getIntHeader (String name): Gets the value of the specified request header and converts it to an integer

<%@page contentType="text/html;charset=GBK" language="java" errorPage=""
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
	"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>11requestTest Form page for collecting parameters</title>
</head>

<body>
<%out.println("this is a request test"); %>
<form id="form1" method="post" action="12request.jsp">
User name:<br/>
<input type="text" name="name" ><hr/>
Gender:<br/>
<input type="radio" name="gender" value="male">
<input type="radio" name="gender" value="female"><hr/>
Favorite colors:<br/>
Red:<input type="checkbox" name="color" value="red">
Green:<input type="checkbox" name="color" value="green">
Blue:<input type="checkbox" name="color" value="blue"><hr/>
Countries from<br/>
<select name="country">
	<option value="China">China</option>
	<option value="U.S.A">U.S.A</option>
	<option value="Russia">Russia</option>
</select><hr/><br/>
<input type="submit" value="Submit">
<input type="reset" value="Reset">

</form>
</body>
</html>
After entering the appropriate information on this page, click the Submit button, and the request parameters represented by the form field will be obtained by the getParameter() method of the request object

4. Not every form field will generate request parameters, but a form field with a name attribute will generate request parameters.

The relationship between form fields and request parameters follows four points:

1. Each form field with a name attribute corresponds to a request parameter

(2) If more than one form field has the same name attribute, only one request parameter is generated from more than one form field, but the parameter has more than one value

(3) The name attribute of the form field specifies the request parameter name, and the value specifies the request parameter value

(4) If a form field has disable="disabled" attribute set, the form field will no longer generate request parameters


The 12request.jsp code is as follows

<%@page contentType="text/html;charset=GBK" language="java" errorPage="" %>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHEML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml-transitional1.dtd">
<html>
<head>
<title>Get Request Header/Request parameters</title>
</head>
<body>
<%
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
	String headerName = headerNames.nextElement();
	out.println(headerName + "--->" + request.getHeader(headerName) + "<br/>");
}
out.println("<hr/>");
request.setCharacterEncoding("gb2312");
String name = request.getParameter("name");
String gender = request.getParameter("gender");
String[] color = request.getParameterValues("color");
String national = request.getParameter("country");

%>

//Your name: <%=name%><hr/>
//Your gender: <%=gender%><hr/>
//Your favorite color: <% for (String c:color){
	out.println(c+" ");
}
%><hr/>
//Your country is: <%=national%>
</body>

</html>

Page display:

host--->localhost:8080
connection--->keep-alive
content-length--->83
cache-control--->max-age=0
accept--->text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
origin--->http://localhost:8080
user-agent--->Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 TheWorld 6
content-type--->application/x-www-form-urlencoded
referer--->http://localhost:8080/20160427jspLearn/html/11requestTest.jsp
accept-encoding--->gzip,deflate,sdch
accept-language--->zh-CN,zh;q=0.8
cookie--->JSESSIONID=B3B27A382435E26B7F2307D56A67E419; COOKIE_SUPPORT=true; SCREEN_NAME=314d4a654258713359436b3d; GUEST_LANGUAGE_ID=zh_CN

Your name: chdsjahkjl Your gender:female Your favorite color: red green blue Your country is: China

5. Clients send requests in two ways

(1) GET-style requests: Enter requests sent by access address directly in the browser address bar or submit a form-sending request. The form element corresponding to the form does not have the method attribute set or the method attribute set to get. These requests are GET-style requests.A GET request converts the name and value of the request parameter to a string and appends it to the original URL, so you can see the request parameter name and value in the address bar.Also, GET requests transfer small amounts of data, usually no larger than 2KB

(2) POST-style requests: This is usually sent by submitting a form (represented by a form HTML element) and requires that the method attribute of the front element be set to post.Posting delivers large amounts of data and generally assumes that the size of the POST request parameter is unrestricted, but often depends on server limitations.Moreover, the request data parameters and their corresponding values transmitted by POST are transmitted in HTML HEADER. Users cannot see the value of the request parameters in the address bar, which is relatively safe.

6. If the parameter to be passed is a normal string and only a few parameters need to be passed, you can choose to send the request parameter by GET. After the request parameter sent by GET is appended to the URL of the address bar, the URL of the address bar will become as follows:

url?param1=value1¶m2=value2&...paramN=valueN:
Can'?'be between URL and parameter?Split, with multiple parameters split by'&'.

<%@page contentType="text/html;charset=GBK" language="java" errorPage=""%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>
//Get GET parameters
</title>
</head>
<body>
<%String name=request.getParameter("name");
String gender=request.getParameter("gender");
%>
//Your name: <%=name%><br/>
//Your gender: <%=gender%><hr/>


</body>
</html>
7. The code for requesting to get POST request parameters is exactly the same as that for getting GET request parameters.When sending a request to this page, add some GET request parameters directly to the address bar.

8. If the request parameters contain non-Western European characters, obtaining these parameters is more complex

<%@page contentType="text/html;charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional //EN
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Gets the GET Request parameters</title>
</head>
<body>
<%
String rawQueryStr = request.getQueryString();
out.println("The original query string is:" + rawQueryStr + "<hr/>");
String queryStr = java.net.URLDecoder.decode(rawQueryStr,"gbk");
out.println("The decoded query string is:" + queryStr + "<hr/>");
String[] paramerters = queryStr.split("&");
for(String paramPair : paramerters){
	out.println("Each request parameter name, value pair is:" + paramPair + "<br/>");
	String[] nameValue = paramPair.split("=");
	out.println(nameValue[0] + "The value of the parameter is:" + nameValue[1] + "<hr/>");
}
%>
</body>
</html>
Page Display Results

The original query string is: name=%C9%B5%B4%F3%B8%F6&gender=female&color=red&country=%D6%D0%B9%FA

The decoded query string is: name=silly&gender=female&color=red&country=China Each request parameter name, value pair is: name=silly
The value of the name parameter is: Silly Each request parameter name, value pair is: gender=female
The value of the gender parameter is female: Each request parameter name, value pair is: color=red
The value of the color parameter is red: Each request parameter name, value pair is: country=China
The value of the count parameter is: China


Note: When getting the Chinese parameter in GET request is worth the key code, in order to get the Chinese parameter value in GET request, you must use the java.net.URLDecoder class (URLDecoder URLEncoder)

// Another way to parse Chinese scrambling
String rawname = request.getParameter("name");
out.println(rawname + "<br/>");
byte[] rawBytes =  rawname.getBytes("ISO-8859-1");
String name = new String(rawBytes,"gb2312");
out.println(name + "<hr/>");
%>

9. Operational request scope properties

HttpServletRequest also includes the following two methods for setting properties to get the request range

setAttribute (String attName, Object attValue) sets attValue as a property of the request range

Object getAttribute(String atName); Gets the properties of the request range

When the forward user requests, the parameters of the request and the properties of the request are not lost.

<%@page contentType="text/html;charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional.dtd">
<html>
<head>
<title>Money withdrawal form page</title>
</head>
<body>
<form method="post" action="16first.jsp">
	//Draw money: <input type="text" name="balance">
	<input type="submit" value="Submit">

</form>
</body>


</html>

<%@page contentType="text/html; charset=GBK" language="java" errorPage=""%>
<%@page import= "java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transition.dtd"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>request Handle</title>
</head>
<body>
<%
String bal = request.getParameter("balance");
double qian = Double.parseDouble(bal);
if(qian < 500){
	out.println("For you" + qian
			+ "block");
	out.println("Account reduction" + qian);
}else{
	List<String> info = new ArrayList<String>();
	info.add("1111111");
	info.add("2222222");
	info.add("3333333");
	request.setAttribute("info",info);
	
%>
<!-- Implement forwarding -->
<jsp:forward page="17second.jsp"/>
<%}%>

</body>

</html>

<%@page contentType="text/html;charset=GBK" language="java" errorPage=""%>
<%@page import="java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C/DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>request Handle</title>
</head>
<body>
<%
String bal = request.getParameter("balance");
double qian = Double.parseDouble(bal);
List<String> info = (List<String>) request.getAttribute("info");
for(String tmp:info){
	out.println(tmp + "<br/>");
}
out.println("Draw money" + qian + "block");
out.println("Account reduction:" + qian);
%>


</body>


</html>

Click on the submitted page to display the results

1111111
2222222
3333333
Less 500.0 account withdrawals: 500.0

When the amount of money requested by a page is greater than 500, the request is forwarded to the 17second.jsp page to be processed, and the value of the balance request parameter can be obtained on the 17second.jsp page, as well as the info property of the request range, indicating that neither the request parameter nor the request range property is lost when the forward user requests.The forward action restores the original request and does not send the request to the server again.

10. Execute forward or include

Request also has the ability to execute forward and include, which is in place of the forward and include action instructions provided by JSP.The forward above is done with the action instructions provided by the JSP, in fact the request object can also execute forward.

The HttpServletRequest class provides a getRequestDispatcher (String path) method, where path is the destination path you want to forward or include, which returns RequestDispatcher

This object provides the following two methods:

forward(ServletRequest request, ServletResponse response): Execute forward

Include (ServletRequest resquest,ServletResponse response): Perform include

Note: When using the request getRequestDispatcher(String path) method, the method path string must be slashed


Posted by tomerg3 on Sat, 15 Jun 2019 10:52:38 -0700