[Jweb] Servlet 如何从上一个页面取得参数. ShowParameters.java

 @1-4  Servlet 编程接口
  * GenericServlet 是所有 Servlet 的鼻祖 (在类的层次上) 在接口层次上 Servlet 是鼻祖
  * 用于 HTTP 的 Servlet 编程都通过继承 javax.servlet.http.HttpServlet 实现
  * 请求处理方法: (分别对应 http 协议的7种请求)
     1. doGet    响应Get请求,常用
     2. doPost   响应Post请求,常用
   * 实例的个数 : 在非分布式的情况下,通常一个Servlet在服务器中有一个实例 
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	HttpServletRequest req  封装了客户端到服务器端的参数值
	doGet() 这个方法是给 Container 调用的。
	 那么当然是由 Container 来负责把你这个形参的值的实参传过来
	  形参要求的是 Interface HttpServletRequest 这个接口
	   那么传递进来的肯定是实现了这个接口的类对象,实现了这个接口就可以调用这个接口的方法!
 @1-5 Tools / video 记录
	2013-12-24
	一、多行注释快捷键
	1.选中你要加注释的区域,用ctrl+shift+C 或者ctrl+/ 会加上//注释
	2.先把你要注释的东西选中,用shit+ctrl+/ 会加上/*    */注释
	3.以上快捷键在重复按一下就会去掉加上的注释
	 07_SERVLET为什么有2个init方法 张志宇精辟解释阅读 tomcat 源码
	 utf-8 一个汉字三个字节表示+
	 09_SERVLET如何从上一个页面取得参数_02
 07_SERVLET为什么有2个init方法 张志宇精辟解释阅读tomcat源码    

TestInitServlet.java    /    public class TestInitServlet extends HttpServlet 

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//注意<init-param>标签的位置
public class TestInitServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=GBK");
        PrintWriter out = resp.getWriter();

        out.println(this.getServletConfig().getServletName());

//      out.println(this.getServletConfig().getInitParameter("name"));
//
        String value1 = this.getServletConfig().getInitParameter("name");
        value1 = new String(value1.getBytes("utf-8"), "GBK");
        out.println(value1);

        java.util.Enumeration params = this.getServletConfig()
                .getInitParameterNames();
        for (; params.hasMoreElements();) {
            String name = params.nextElement().toString();
            String value = this.getServletConfig().getInitParameter(name);
            value = new String(value.getBytes("utf-8"), "GBK");
            out.println(value);
        }
        out.close();
    }
}

// <servlet>
// <servlet-name>TestInitServletaaa</servlet-name>
// <servlet-class>TestInitServlet</servlet-class>
// <init-param>
// <param-name>name</param-name>
// <param-value>小王</param-value>
// </init-param>
// <init-param>
// <param-name>gender</param-name>
// <param-value>男</param-value>
// </init-param>
// </servlet>

ThreeParams.htm

<form name="form1" method="post" action="ThreeParams">
  <table width="343" border="1">
    <tr>
      <td width="92">param1</td>
      <td width="94"><input name="param1" type="text"/>      </td>
    </tr>
    <tr>
      <td>param2</td>
      <td><label>
        <input name="param2" type="text"/>
      </label></td>
    </tr>
    <tr>
      <td>param3</td>
      <td><input name="param3" type="text"/></td>
    </tr>
    <tr>
      <td> </td>
      <td><label>
        <input type="submit" value="提交" />
      </label></td>
    </tr>
  </table>
  </form>
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

//读取三个参数

public class ThreeParams extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=GBK");
//      解决post方式提交内容的乱码
//      一定要写在存取第一个参数前       
//      不要调用 response.setCharacterEncoding("GBK");
        request.setCharacterEncoding("GBK");
        
//      解决get方式乱码问题:修改-->server.xml --> URLEncoding="GBK"
        
        PrintWriter out = response.getWriter();
        out.println(request.getParameter("param1"));
        out.println("<br>");
        out.println(request.getParameter("param2"));
        out.println("<br>");
        out.println(request.getParameter("param3"));
        out.println("<br>");
        System.out.println("in doGet");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}


ShowParametersForm.htm

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

//获取所有的参数

public class ShowParameters extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        String title = "Reading All Request Parameters";
        out.println("<html><head><title>读取所有参数</title></head>"
                + "<BODY BGCOLOR='#FDF5E6'>\n" + "<H1 ALIGN=CENTER>" + title
                + "</H1>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n"
                + "<TR BGCOLOR=\"#FFAD00\">\n"
                + "<TH>Parameter Name<TH>Parameter Value(s)");
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            out.print("<TR><TD>" + paramName + "\n<TD>");
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                    out.println("<I>No Value</I>");
                else
                    out.println(paramValue);
            } else {
                out.println("<UL>");
                for (int i = 0; i < paramValues.length; i++) {
                    out.println("<LI>" + paramValues[i]);
                }
                out.println("</UL>");
            }
        }
        out.println("</TABLE>\n</BODY></HTML>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}
---
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
  <TITLE>A Sample FORM using POST</TITLE>
</HEAD>
<BODY BGCOLOR="#FDF5E6">
<H1 ALIGN="CENTER">A Sample FORM using POST</H1>

<FORM ACTION="ShowParameters" 
      METHOD="POST">
  Item Number: <INPUT TYPE="TEXT" NAME="itemNum"><BR>
  Quantity: <INPUT TYPE="TEXT" NAME="quantity"><BR>
  Price Each: <INPUT TYPE="TEXT" NAME="price" VALUE="$"><BR>
  <HR>
  First Name: <INPUT TYPE="TEXT" NAME="firstName"><BR>
  Last Name: <INPUT TYPE="TEXT" NAME="lastName"><BR>
  Middle Initial: <INPUT TYPE="TEXT" NAME="initial"><BR>
  Shipping Address:
  <TEXTAREA NAME="address" ROWS=3 COLS=40></TEXTAREA><BR>
  Credit Card:<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Visa">Visa<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Master Card">Master Card<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Amex">American Express<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Discover">Discover<BR>
    <INPUT TYPE="RADIO" NAME="cardType"
                     VALUE="Java SmartCard">Java SmartCard<BR>
  Credit Card Number: 
  <INPUT TYPE="PASSWORD" NAME="cardNum"><BR>
  Repeat Credit Card Number:
  <INPUT TYPE="PASSWORD" NAME="cardNum"><BR><BR>
  <CENTER>
    <INPUT TYPE="SUBMIT" VALUE="Submit Order">
  </CENTER>
</FORM>

</BODY>
</HTML>
 ShowParametersMap extends HttpServlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

//获取所有的参数

public class ShowParametersMap extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        String title = "Reading All Request Parameters";
        out.println("<html><head><title>读取所有参数</title></head>"
                + "<BODY BGCOLOR='#FDF5E6'>\n" + "<H1 ALIGN=CENTER>" + title
                + "</H1>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n"
                + "<TR BGCOLOR=\"#FFAD00\">\n"
                + "<TH>Parameter Name<TH>Parameter Value(s)");
        Map<String, String[]> paramMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> entries = paramMap.entrySet(); 
        for(Iterator<Map.Entry<String, String[]>> it = entries.iterator(); it.hasNext(); ) {
            Map.Entry<String, String[]> entry = it.next();
            String paramName = entry.getKey();
            out.print("<TR><TD>" + paramName + "\n<TD>");
            String[] paramValues = entry.getValue();
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() == 0)
                    out.println("<I>No Value</I>");
                else
                    out.println(paramValue);
            } else {
                out.println("<UL>");
                for (int i = 0; i < paramValues.length; i++) {
                    out.println("<LI>" + paramValues[i]);
                }
                out.println("</UL>");
            }
        }
        out.println("</TABLE>\n</BODY></HTML>");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}


posted @ 2013-12-24 16:00  小尼人00  阅读(196)  评论(0编辑  收藏  举报