张志宇 SERVELET JSP学习讲义-08_SERVLET如何从上一个页面取得参数

首先,servlet如下编写代码:

package com.zzk.cn;


import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;

public class ThreeParams extends HttpServlet{

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=GBK");
		//解决post方式提交内容的乱码
		//一定要写在存取第一个参数之前
		request.setCharacterEncoding("GBK");
		
		//解决get方式乱码问题:-->URIEncoding="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");
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("in doPost");
		doGet(request, response);
	}
}


页面输出:

1
2是


ThreeParams.htm
里post方式

<form id="form1" 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" id="param1" />      </td>
    </tr>
    <tr>
      <td>param2</td>
      <td><label>
        <input name="param2" type="text" id="param2" />
      </label></td>
    </tr>
    <tr>
      <td>param3</td>
      <td><input name="param3" type="text" id="param3" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><label>
        <input type="submit" value="提交" />
      </label></td>
    </tr>
  </table>
  </form>


传参成功

 tomcat里显示:

in Dopost
in Doget

然后将html改为get方式:


传参成功

tomcat里显示:

in doGet




你可能感兴趣的:(张志宇 SERVELET JSP学习讲义-08_SERVLET如何从上一个页面取得参数)