Example analysis of various methods used in the Request object in Java _java

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.