The use of servletrequest,servletresponse in Javaweb and simplifying the servlet approach

Source: Internet
Author: User

First, let's talk about how to use the Servletrequest,servletresponse class:

public void Service (ServletRequest request, servletresponse response)
Throws Servletexception, IOException {
/*
* How to Get request information in servlet:
* (1): Servlet's Servlet () method is used to answer requests: Because each request invokes the servlet () method;
* ServletRequest: Encapsulates the request information. Any request information that can be obtained from it.
* Servletresponse: Encapsulates the response information, if you want to give the user what response, you can use the method of the interface implementation.
* The implementation classes for both interfaces are implemented by the server and are passed in when the server invokes the service method.
*
* ServletRequest: Encapsulates the request information. Any request information that can be obtained from it.
* */
SYSTEM.OUT.PRINTLN ("request came");
SYSTEM.OUT.PRINTLN (Request);
SYSTEM.OUT.PRINTLN (response);

Get request Parameter: String GetParameter (string name): Returns the parameter value based on the name of the request parameter.
If the request parameter has more than one value (for example, a checkbox), the method can only get the value to the first commit.

Get user and password in login.html
String user=request.getparameter ("user");
String password=request.getparameter ("password");

System.out.println (user+ "---" +password);

String[] Getparametervalues (string name): Returns an array of strings corresponding to the request parameters, based on the name of the request parameter.

Gets the multiple values of the check box in login.html, where the name value in the check box is equal
String inte=request.getparameter ("interesting");
System.out.println (inte);
String[] Interestings=request.getparametervalues ("interesting");
for (String inter:interestings) {
SYSTEM.OUT.PRINTLN ("--->" +inter);
}

Enumeration Getparameternames (): Returns the enumeration object that corresponds to the parameter name,
A Getinitparameternames () method similar to ServletConfig (or ServletContext).
Enumeration<string> Names=request.getparameternames ();
while (Names.hasmoreelements ()) {
String name=names.nextelement ();
String Value=request.getparameter (name);

System.out.println ("Name:" +name+ "---" + "value:" +value);
}

Map Getparametermap (): Returns the key-value pair for the request parameter: key: Parameter name, Value: Parameter value, String array type.
This method can put all value values with the same key value together to output
Map<string, string[]> map=request.getparametermap ();
For (map.entry<string, string[]> entry:map.entrySet ()) {
System.out.println (Entry.getkey () + "---" "+arrays.aslist (Entry.getvalue ()));
}

HttpServletRequest is a sub-interface of ServletRequest,
defined for the HTTP request. It contains a number of methods related to getting HTTP requests.
HttpServletRequest http= (httpservletrequest) request;

Gets the URL of the request,
String Requesturl=http.getrequesturi ();
System.out.println (Requesturl);

Get Request method: Two kinds of request mode post and get
String Method=http.getmethod ();
System.out.println (method);

Gets the mapped path of the requested Serlvet
String Servletpath = Http.getservletpath ();
System.out.println (Servletpath); /loginservlet

Gets the requested query string GET request? The following string
String querystring=http.getquerystring ();
System.out.println (queryString);



Servletresponse encapsulates the response information, and if you want to respond to the user, you can use the interface method to implement
No need to look at the console, directly on the page so that users can see

Set the appropriate type, such as a Word document
Response.setcontenttype ("Application/msword");//application/msword is a Word document

Getwriter (): Returns the PrintWriter object. Calling the object's print () method prints the parameters in print () directly to the client's browser.
PrintWriter Out=response.getwriter ();

String User1=getservletcontext (). Getinitparameter ("user");
String Password1=getservletcontext (). Getinitparameter ("password");
System.out.println ("User1:" +user1+ "---" + "password1" +password1);
if (User.equals (user1) && password.equals (Password1)) {
Out.println ("HelloWorld ...");
}


void Sendredirect (String location): The requested redirect. (This method is defined in HttpServletResponse.)


}

----------------------------------------------------------------------------------------------------

Simplify classes that inherit the Servlet interface: Transform this class into an abstract method, and then let the other subclasses inherit the abstract parent class

The abstract class changed in: Mygenericservlet

/*
* Customize the implementation class for a servlet interface: let the development of any servlet inherit the class to simplify development
* Define it as an abstract class with an abstract method
* */
Public abstract class Mygenericservlet implements servlet,servletconfig{
These are the methods of the Servlet interface
@Override
public void Destroy () {}

@Override
Public ServletConfig Getservletconfig () {
return servletconfig;
}

public void Init () throws Servletexception {
TODO auto-generated Method Stub

}

@Override
Public String Getservletinfo () {
return null;
}

Private ServletConfig ServletConfig;

@Override
public void init (ServletConfig servletconfig) throws Servletexception {
This.servletconfig=servletconfig;

}

This method must be implemented, so it becomes an abstract method
@Override
public abstract void Service (ServletRequest arg0, Servletresponse arg1)
Throws Servletexception, IOException;


Here's how to servletconfig the interface:
@Override
public string Getinitparameter (string arg0) {
Return Servletconfig.getinitparameter (arg0);
}

@Override
Public enumeration Getinitparameternames () {
return Servletconfig.getinitparameternames ();
}

@Override
Public ServletContext Getservletcontext () {
return Servletconfig.getservletcontext ();
}

@Override
Public String Getservletname () {
return Servletconfig.getservletname ();
}

}

-----------------------

Subclasses inherit the abstract parent class:

public class Loginservlet extends mygenericservlet{

Initializing the current servlet: Overriding the Init method
@Override
public void Init () throws servletexception{
SYSTEM.OUT.PRINTLN ("Initialize >>>>");
}
@Override
public void Service (ServletRequest request, servletresponse response)
Throws Servletexception, IOException {
/*
* How to Get request information in servlet:
* (1): Servlet's Servlet () method is used to answer requests: Because each request invokes the servlet () method;
* ServletRequest: Encapsulates the request information. Any request information that can be obtained from it.
* Servletresponse: Encapsulates the response information, if you want to give the user what response, you can use the method of the interface implementation.
* The implementation classes for both interfaces are implemented by the server and are passed in when the server invokes the service method.
*
* ServletRequest: Encapsulates the request information. Any request information that can be obtained from it.
* */
SYSTEM.OUT.PRINTLN ("request came");
SYSTEM.OUT.PRINTLN (Request);
SYSTEM.OUT.PRINTLN (response);

Get request Parameter: String GetParameter (string name): Returns the parameter value based on the name of the request parameter.
If the request parameter has more than one value (for example, a checkbox), the method can only get the value to the first commit.

Get user and password in login.html
String user=request.getparameter ("user");
String password=request.getparameter ("password");

System.out.println (user+ "---" +password);

String[] Getparametervalues (string name): Returns an array of strings corresponding to the request parameters, based on the name of the request parameter.

Gets the multiple values of the check box in login.html, where the name value in the check box is equal
String inte=request.getparameter ("interesting");
System.out.println (inte);
String[] Interestings=request.getparametervalues ("interesting");
for (String inter:interestings) {
SYSTEM.OUT.PRINTLN ("--->" +inter);
}

Enumeration Getparameternames (): Returns the enumeration object that corresponds to the parameter name,
A Getinitparameternames () method similar to ServletConfig (or ServletContext).
Enumeration<string> Names=request.getparameternames ();
while (Names.hasmoreelements ()) {
String name=names.nextelement ();
String Value=request.getparameter (name);

System.out.println ("Name:" +name+ "---" + "value:" +value);
}

Map Getparametermap (): Returns the key-value pair for the request parameter: key: Parameter name, Value: Parameter value, String array type.
This method can put all value values with the same key value together to output
Map<string, string[]> map=request.getparametermap ();
For (map.entry<string, string[]> entry:map.entrySet ()) {
System.out.println (Entry.getkey () + "---" "+arrays.aslist (Entry.getvalue ()));
}

HttpServletRequest is a sub-interface of ServletRequest,
defined for the HTTP request. It contains a number of methods related to getting HTTP requests.
HttpServletRequest http= (httpservletrequest) request;

Gets the URL of the request,
String Requesturl=http.getrequesturi ();
System.out.println (Requesturl);

Get Request method: Two kinds of request mode post and get
String Method=http.getmethod ();
System.out.println (method);

Gets the mapped path of the requested Serlvet
String Servletpath = Http.getservletpath ();
System.out.println (Servletpath); /loginservlet

Gets the requested query string GET request? The following string
String querystring=http.getquerystring ();
System.out.println (queryString);



Servletresponse encapsulates the response information, and if you want to respond to the user, you can use the interface method to implement
No need to look at the console, directly on the page so that users can see

Set the appropriate type, such as a Word document
Response.setcontenttype ("Application/msword");//application/msword is a Word document

Getwriter (): Returns the PrintWriter object. Calling the object's print () method prints the parameters in print () directly to the client's browser.
PrintWriter Out=response.getwriter ();

String User1=getservletcontext (). Getinitparameter ("user");
String Password1=getservletcontext (). Getinitparameter ("password");
System.out.println ("User1:" +user1+ "---" + "password1" +password1);
if (User.equals (user1) && password.equals (Password1)) {
Out.println ("HelloWorld ...");
}


void Sendredirect (String location): The requested redirect. (This method is defined in HttpServletResponse.)


}

}

-----------------------------------------------------------------------------------------------

The above abstract class is just genericservlet abstract class, and subclasses inherit genericservletc abstract class directly;

Where Genericservlet, is a servlet. is the implementation class for the Servlet interface and the ServletConfig interface, but an abstract class. Where the servlet method is an abstract method.

If the new Servlet class inherits directly from the Genericservlet abstract class, it is easier to develop.

Specific implementation:

①. A member variable of type Serlvetconfig is declared in Genericservlet and initialized in the init (ServletConfig) method.
②. The method of ServletConfig interface is realized by using ServletConfig member variable method
③. An init () method is also defined, which is called in the init (Serlvetconfig) method, and the subclass can directly override Init () in which the Servlet initialization is implemented.
④. It is not recommended to overwrite Init (servletconfig) directly, because if you forget to write Super.init (config); And still using the Serlvetconfig interface method,
A null pointer exception appears.
⑤. New init () {} is not a life-cycle method of Serlvet. and init (ServletConfig) is a life-cycle-related approach.

public class Loginservlet extends genericservlet{

Initializing the current servlet: Overriding the Init method
@Override
public void Init () throws servletexception{
SYSTEM.OUT.PRINTLN ("Initialize >>>>");
}
@Override
public void Service (ServletRequest request, servletresponse response)
Throws Servletexception, IOException {
/*
* How to Get request information in servlet:
* (1): Servlet's Servlet () method is used to answer requests: Because each request invokes the servlet () method;
* ServletRequest: Encapsulates the request information. Any request information that can be obtained from it.
* Servletresponse: Encapsulates the response information, if you want to give the user what response, you can use the method of the interface implementation.
* The implementation classes for both interfaces are implemented by the server and are passed in when the server invokes the service method.
*
* ServletRequest: Encapsulates the request information. Any request information that can be obtained from it.
* */
SYSTEM.OUT.PRINTLN ("request came");
SYSTEM.OUT.PRINTLN (Request);
SYSTEM.OUT.PRINTLN (response);

Get request Parameter: String GetParameter (string name): Returns the parameter value based on the name of the request parameter.
If the request parameter has more than one value (for example, a checkbox), the method can only get the value to the first commit.

Get user and password in login.html
String user=request.getparameter ("user");
String password=request.getparameter ("password");

System.out.println (user+ "---" +password);

String[] Getparametervalues (string name): Returns an array of strings corresponding to the request parameters, based on the name of the request parameter.

Gets the multiple values of the check box in login.html, where the name value in the check box is equal
String inte=request.getparameter ("interesting");
System.out.println (inte);
String[] Interestings=request.getparametervalues ("interesting");
for (String inter:interestings) {
SYSTEM.OUT.PRINTLN ("--->" +inter);
}

Enumeration Getparameternames (): Returns the enumeration object that corresponds to the parameter name,
A Getinitparameternames () method similar to ServletConfig (or ServletContext).
Enumeration<string> Names=request.getparameternames ();
while (Names.hasmoreelements ()) {
String name=names.nextelement ();
String Value=request.getparameter (name);

System.out.println ("Name:" +name+ "---" + "value:" +value);
}

Map Getparametermap (): Returns the key-value pair for the request parameter: key: Parameter name, Value: Parameter value, String array type.
This method can put all value values with the same key value together to output
Map<string, string[]> map=request.getparametermap ();
For (map.entry<string, string[]> entry:map.entrySet ()) {
System.out.println (Entry.getkey () + "---" "+arrays.aslist (Entry.getvalue ()));
}

HttpServletRequest is a sub-interface of ServletRequest,
defined for the HTTP request. It contains a number of methods related to getting HTTP requests.
HttpServletRequest http= (httpservletrequest) request;

Gets the URL of the request,
String Requesturl=http.getrequesturi ();
System.out.println (Requesturl);

Get Request method: Two kinds of request mode post and get
String Method=http.getmethod ();
System.out.println (method);

Gets the mapped path of the requested Serlvet
String Servletpath = Http.getservletpath ();
System.out.println (Servletpath); /loginservlet

Gets the requested query string GET request? The following string
String querystring=http.getquerystring ();
System.out.println (queryString);



Servletresponse encapsulates the response information, and if you want to respond to the user, you can use the interface method to implement
No need to look at the console, directly on the page so that users can see

Set the appropriate type, such as a Word document
Response.setcontenttype ("Application/msword");//application/msword is a Word document

Getwriter (): Returns the PrintWriter object. Calling the object's print () method prints the parameters in print () directly to the client's browser.
PrintWriter Out=response.getwriter ();

String User1=getservletcontext (). Getinitparameter ("user");
String Password1=getservletcontext (). Getinitparameter ("password");
System.out.println ("User1:" +user1+ "---" + "password1" +password1);
if (User.equals (user1) && password.equals (Password1)) {
Out.println ("HelloWorld ...");
}


void Sendredirect (String location): The requested redirect. (This method is defined in HttpServletResponse.)


}

}

The use of servletrequest,servletresponse in Javaweb and simplifying the servlet approach

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.