通过request读取所有参数的练习

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;

public class ShowParameters extends HttpServlet{

	@Override
	protected 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 Values</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>");
		}
		
	}
	}

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

以上是通过request读取所有数据的示例程序


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitinal//EN">
<HTML>
<HEAD>
	<TITLE>A Sample FORM using POST</TITLE>
</HEAD>
<BODY BECOLOR="#FDF5E6">
<H1 ALIGN="CENTER>A Sample FORM using POST</H1>

<FORM ACTION="servlet/ShowParameters"
		METHOD="get">
	Item Number:<INPUT TYPE="TEXT" NAEM="itemNum"><BR>
	Quantity: <INPUT TYPE="TEXT" NAEM="quantity"><BR>
	Price Each: <INPUT TYPE="TEXT" NAEM="price " VALUE="$"><BR>
	<HR>
	First Name: <INPUT TYPE="TEXT" NAEM="firstName"><BR>
	Lase Name: <INPUT TYPE="TEXT" NAEM="lastName"><BR>
	Middle Initial: <INPUT TYPE="TEXT" NAEM="initial"><BR>
	Shipping Address:
	<TEXTAREA NAME="address" ROWS=3 COLS=40></TEXTAREA><BR>
	Credit Card:
	  <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><span style="white-space:pre">	</span>
该段程序的功能可以显示一个网页的静态文本的代码


可以通过以上两种方式访问网页
1.静态访问方式  http://127.0.0.1:8888/test/servlet/ShowParametersForm
<span style="white-space:pre">		</span>使用webapp的跟路径,后加文件名的方式
2.使用提交的方式访问页面  http://127.0.0.1:8888/test/ShowParametersForm.htm


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值