Java learning notes-HttpServletResponse (21), java learning notes

Source: Internet
Author: User

Java learning notes-HttpServletResponse (21), java learning notes

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// get the byte stream object ServletOutputStream out = response. getOutputStream (); // gets the response Stream object PrintWriter writer = response. getWriter (); out. write ("image ". getBytes (); writer. write ("<br/> text ");}

The running result is abnormal. The exception information is as follows:

java.lang.IllegalStateException: getOutputStream() has already been called for this response

Summary:

Because the server output can use a byte stream or a batch stream, you only need to obtain one of the streams in actual development.

HttpServletRequest Interface

This interface is inherited from the ServletRequest interface. The object of this interface is mainly used to obtain data in user submitted requests.

The object of this interface is directly transmitted to the service () method created by the Web Container (Tomcat.

Common Methods of ServletRequest APIs

Get user request parameters

String getParameter (String name) Get a single request parameter String [] getParameterValues (String name) get multiple request values Enumeration getParameterNames () Get all parameter names Map getParameterMap () obtains the Map set object of all parameters.

Store and retrieve data in objects

Void setAttribute (String name, Object o) is used to set the attribute Object getAttribute (String name) to obtain the attribute.

Encoding method used to obtain user request parameter data

String getCharacterEncoding () sets the encoding method for obtaining request parameter values

Common Methods of HttpServletRequest APIs

Cookie [] getCookies () get Cookie array HttpSession getSession () Get Session Object String getContextPath () Get website ing path
GET Request Parameters

In actual development, the main task of the request object is to obtain various types of parameters passed by the user. The following describes how to obtain Request Parameters in different ways.

1. Obtain a single request parameter

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// obtain a single request parameter String value = request. getParameter ("uname"); // get the byte stream object ServletOutputStream out = response. getOutputStream (); String data = "value =" + value; // output out. write (data. getBytes ());}

2. Obtain multiple values.

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// obtain a single request parameter String value [] = request. getParameterValues ("likes"); // get the byte stream object ServletOutputStream out = response. getOutputStream (); String data = "value =" + Arrays. toString (value ). toString (); // output out. write (data. getBytes ());}

3. Obtain all request data

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// iterator for obtaining all request parameter names Enumeration <String> nas = request. getParameterNames (); // get the byte stream object ServletOutputStream out = response. getOutputStream (); // iteration while (nas. hasMoreElements () {String name = nas. nextElement (); if ("uname ". equals (name) {String value = request. getParameter (name); out. write (name + "=" + value ). getBytes ();} out. write ("<br/> ". getBytes (); if ("likes ". equals (name) {String [] value = request. getParameterValues (name); out. write (name + "=" + Arrays. toString (value )). getBytes ());}}}

4. encapsulate Request Parameters

Generally, the registration page contains a large amount of user request data. If the servlet directly obtains the data, but the servlet itself cannot process the data, it must be passed to the underlying business layer code for processing. It is too cumbersome to transmit data one by one. Therefore, after obtaining the user's request parameters, you must properly compress the data so that it can be passed to the next layer for data processing.

JavaBean à BeanUtils

Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// Map Object for obtaining parameters of all requests Map <String, Object> map = request. getParameterMap (); // obtain the set Set <Map. entry <String, Object> set = map. entrySet (); // gets the Iterator object Iterator <Map. entry <String, Object> it = set. iterator (); // create a javabean Object User user = new User (); // loop while (it. hasNext () {Map. entry <Strin G, Object> entry = it. next (); String name = entry. getKey (); Object value = entry. getValue (); try {BeanUtils. setProperty (user, name, value);} catch (Exception e) {System. out. println ("encapsulation failed! "); E. printStackTrace () ;}// output user object data System. out. println (user );}

 

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.