This example describes the use of various methods of the request object in Java. Share to everyone for your reference, specific as follows:
The request object is to issue requests from the client to the server, including information submitted by the user and some information from the client. The request object is an implementation instance of the Javax.servlet.http.HttpServletRequest class.
The request object encapsulates the browser's requested information, and the various methods of the request object allow you to obtain information about the client and the requests submitted by the user.
The common way to get client-submitted request parameters using the Request object is as follows:
1.String getparameter (string name), gets the parameter value of the client, returns the value of the specified parameter as a string , and returns a null value if the argument does not exist. Use this method when passing parameters in a form, link, or URL bar.
For example, get the parameter values for client name:
Copy Code code as follows:
String name = Request.getparameter ("name");
2.string[] Getparametervalues (String name), gets all parameter values for a single parameter, primarily for the value of the check box, the return value type is a string array string[]
For example, get all the values for the client hobby check box:
string[] Hobbys = request.getparametervalues ("hobby");
if (Hobbys!= null)
{
out.println ("Your hobby has:");
for (int i=0;i
3.void setcharacterencoding (String encoding), set character encoding method, to solve the problem of passing characters of non-English characters.
For example
Copy Code code as follows:
Request.setcharacterencoding ("UTF-8");
Instance: Using the Request object to implement the user registration feature
Zhuce.html source code is as follows:
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
The
zhuce.jsp source code is as follows:
<%@ page language= "java" import= "java.util.*" contenttype= "Text/html;charset=utf-8"%> <% String Path = Request
. Getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
I hope this article will help you with your Java programming.