Servlet与Jsp学习笔记--2、POST HTTP Request

Problem

POST request.

Solution

Use the ServletRequest.getParameter(String name), getParameterMap( ), getParameterNames( ), or getParameterValues(String name) methods in the servlet's doPost method

Code(servlet)

import java.io.IOException; 

import java.io.PrintWriter;    

 

import java.util.Enumeration;

 

import javax.servlet.ServletException;

 

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.util.Map;

import java.util.Iterator;

import java.util.Map.Entry;

 

public class FirstServlet extends HttpServlet {

   

 public void doGet(HttpServletRequest request,

    HttpServletResponse response) throws ServletException,

      java.io.IOException {

   

    //set the MIME type of the response, "text/html"

    response.setContentType("text/html");

   

    //use a PrintWriter to send text data to the client who has requested the

    //servlet

    java.io.PrintWriter out = response.getWriter( );

 

    //Begin assembling the HTML content

    out.println("<html><head>");

   

    out.println("<title>Help Page</title></head><body>");

    out.println("<h2>Please submit your information</h2>");

  

   //make sure method="post" so that the servlet service method

   //calls doPost in the response to this form submit

    out.println(

        "<form method=""post"" action =""" + request.getContextPath( ) +

            "/firstservlet"" >");

 

    out.println("<table border=""0""><tr><td valign=""top"">");

    out.println("Your username: </td> <td valign=""top"">");

    out.println("<input type=""text"" name=""username"" size=""20"">");

    out.println("</td></tr><tr><td valign=""top"">");

    out.println("Your department: </td> <td valign=""top"">");

    out.println("<input type=""text"" name=""department"" size=""20"">");

    out.println("</td></tr><tr><td valign=""top"">");

    out.println("Your email: </td> <td valign=""top"">");

    out.println("<input type=""text"" name=""email"" size=""20"">");

    out.println("</td></tr><tr><td valign=""top"">");

 

    out.println("<input type=""submit"" value=""Submit Info""></td></tr>");

    out.println("</table></form>");

    out.println("</body></html>");

 

    }//doGet

 public void doPost(HttpServletRequest request,

    HttpServletResponse response) throws ServletException,

    java.io.IOException {

   

    //display the parameter names and values

    Enumeration paramNames = request.getParameterNames( );

 

    String parName;//this will hold the name of the parameter

   

    boolean emptyEnum = false;

 

    if (! paramNames.hasMoreElements( ))

        emptyEnum = true;

   

       

    //set the MIME type of the response, "text/html"

    response.setContentType("text/html");

   

    //use a PrintWriter to send text data to the client

    java.io.PrintWriter out = response.getWriter( );

   

    //Begin assembling the HTML content

    out.println("<html><head>");

    out.println("<title>Submitted Parameters</title></head><body>");

   

    if (emptyEnum){

        out.println(

           "<h2>Sorry, the request does not contain any parameters</h2>");

    } else {

    out.println(

        "<h2>Here are the submitted parameter values</h2>");

    }

   

    while(paramNames.hasMoreElements( )){

   

        parName = (String) paramNames.nextElement( );

 

        out.println(

            "<strong>" + parName + "</strong> : " +

                request.getParameter(parName));

 

        out.println("<br />");

 

    }//while

 

   String name = request.getParameter("username");

    String depart = request.getParameter("department");

    String email = request.getParameter("email");

out.println(

        "Your name is: " + ( (name == null || name.equals("")) ?

             "Unknown" : name));

 

    out.println("<br><br>");

 

    out.println(

       "Your department is: " + ( (depart == null || depart.equals("")) ?

           "Unknown" : depart));

 

         out.println("<br><br>");

 

    out.println(

        "Your email address is: " + ( (email == null || 

            email.equals("")) ? "Unknown" : email));

 

Map param_map = request.getParameterMap( );

 

    if (param_map == null)

        throw new ServletException(

            "getParameterMap returned null in: " +

                getClass( ).getName( ));

 

        Iterator iterator = param_map.entrySet( ).iterator( );

        while(iterator.hasNext( )){

            Map.Entry me = (Map.Entry)iterator.next( );

            out.println(me.getKey( ) + ": ");

            String[] arr = (String[]) me.getValue( );

 

            for(int i=0;i<arr.length;i++){

                out.println(arr[i]);

                //print commas after multiple values,

                //except for the last one

                if (i > 0 && i != arr.length-1)

                out.println(", ");

            }//end for

 

                out.println("<br><br>");

        }//end while

 

       

    out.println("</body></html>");

   

 }// doPost

}

 

Code(单一jsp)

<%@page contentType="text/html"%>

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<html>

<head><title>Post Data Viewer</title></head>

<body>

<h2>Here is your posted data</h2>

 

<FORM METHOD=POST ACTION="">

         <INPUT TYPE="text" NAME="name">

<INPUT TYPE="text" NAME="department"><INPUT TYPE="submit">

</FORM>

<c:forEach var="map_entry" items="${param}">

    <strong><c:out value="${map_entry.key}" /></strong>:

    <c:out value="${map_entry.value}" /><br><br>

</c:forEach>

 

</body>

</html>

 

 

Code(Beanjsp)

package pk;

 

 

public class UserBean implements java.io.Serializable{

 

String username;

String email;

String department;

 

public UserBean( ){}

 

public void setUsername(String _username){

 

    if(_username != null && _username.length( ) > 0)

        username = _username;

    else

         username = "Unknown";

}

 

public String getUsername( ){

 

    if(username != null)

        return username;

    else

        return "Unknown";}

 

public void setEmail(String _email){

 

     if(_email != null && _email.length( ) > 0)

        email = _email;

    else

         email = "Unknown";

}

 

public String getEmail( ){

 

 if(email != null)

        return email;

    else

        return "Unknown";}

 

public void setDepartment(String _department){

 

     if(_department != null && _department.length( ) > 0)

        department = _department;

    else

         department = "Unknown";

}

 

public String getDepartment( ){

 

    if(department != null)

        return department;

    else

        return "Unknown"; }

 

}

 

部署到"WEB-INF"classes"pk

javac -d WEB-INF/classes UserBean.java

 

jsp:

<%@page contentType="text/html"%>

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<%@page import="pk.*"%>

<jsp:useBean id="userB" class="UserBean" >

 

<jsp:setProperty name="userB" property="*" />

 

</jsp:useBean>

<html>

<head><title>Post Data Viewer</title></head>

<body>

 

<FORM METHOD=POST ACTION="">

         <INPUT TYPE="text" NAME="username">

<INPUT TYPE="text" NAME="department">

<INPUT TYPE="text" NAME="email">

<INPUT TYPE="submit">

</FORM>

 

<h2>Here is your posted data</h2>

 

    <strong>User name</strong>:

    <c:out value="${userB.username}" /><br><br>

 

     <strong>Department</strong>:

    <c:out value="${userB.department}" /><br><br>

 

     <strong>Email</strong>:

    <c:out value="${userB.email}" />

 

</body>

</html>

 

你可能感兴趣的:(servlet)