String departName1 =
new String(getRequest().getParameter("departName").getBytes("ISO-8859-1"),"UTF-8");
request.setCharacterEncoding("GBK"); 可以处理地址栏的中文
但是request.setCharacterEncoding("UTF-8");确不能直接处理地址栏的中文
String a = new String(param.getBytes("ISO-8859-1"),"GBK");
http://blog.csdn.net/bohemia/archive/2005/12/10/548706.aspx
servlet获取get或者post的参数
简单小程序,就是在页面里显示出POST或者GET
1,request.getMethod(),获得调用页面的方法
2,Enumeration<String> e = request.getParameterNames();接收所有的参数的名称,并且存在一个Enumeration里
3,parameterValue = request.getParameter(parameterName); 循环这句话,获取所有的参数的值
1,request.getMethod(),获得调用页面的方法
2,Enumeration<String> e = request.getParameterNames();接收所有的参数的名称,并且存在一个Enumeration里
3,parameterValue = request.getParameter(parameterName); 循环这句话,获取所有的参数的值
package
com.fcp.web2;
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 ShowParameter extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = - 3484338400290863566L ;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( " text/html " );
PrintWriter out = response.getWriter();
out
.println( " <!DOCTYPE HTML PUBLIC / " - // W3C // DTD HTML 4.01 Transitional // EN/">");
out.println( " <HTML> " );
out.println( " <HEAD><TITLE>A Servlet</TITLE></HEAD> " );
out.println( " <BODY> " );
this .printMethod(request, response);
this .printParameters(request, response);
out.println( " </BODY> " );
out.println( " </HTML> " );
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( " text/html " );
PrintWriter out = response.getWriter();
out
.println( " <!DOCTYPE HTML PUBLIC / " - // W3C // DTD HTML 4.01 Transitional // EN/">");
out.println( " <HTML> " );
out.println( " <HEAD><TITLE>A Servlet</TITLE></HEAD> " );
out.println( " <BODY> " );
this .printMethod(request, response);
this .printParameters(request, response);
out.println( " </BODY> " );
out.println( " </HTML> " );
out.flush();
out.close();
}
private void printMethod(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out = response.getWriter();
out.println( " Method: " + request.getMethod() + " <br> " );
}
private void printParameters(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out = response.getWriter();
Enumeration < String > e = request.getParameterNames();
String parameterName, parameterValue;
while (e.hasMoreElements())
{
parameterName = e.nextElement();
parameterValue = request.getParameter(parameterName);
out.println(parameterName + " : " + parameterValue + " <br> " );
}
}
}
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 ShowParameter extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = - 3484338400290863566L ;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( " text/html " );
PrintWriter out = response.getWriter();
out
.println( " <!DOCTYPE HTML PUBLIC / " - // W3C // DTD HTML 4.01 Transitional // EN/">");
out.println( " <HTML> " );
out.println( " <HEAD><TITLE>A Servlet</TITLE></HEAD> " );
out.println( " <BODY> " );
this .printMethod(request, response);
this .printParameters(request, response);
out.println( " </BODY> " );
out.println( " </HTML> " );
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( " text/html " );
PrintWriter out = response.getWriter();
out
.println( " <!DOCTYPE HTML PUBLIC / " - // W3C // DTD HTML 4.01 Transitional // EN/">");
out.println( " <HTML> " );
out.println( " <HEAD><TITLE>A Servlet</TITLE></HEAD> " );
out.println( " <BODY> " );
this .printMethod(request, response);
this .printParameters(request, response);
out.println( " </BODY> " );
out.println( " </HTML> " );
out.flush();
out.close();
}
private void printMethod(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out = response.getWriter();
out.println( " Method: " + request.getMethod() + " <br> " );
}
private void printParameters(HttpServletRequest request, HttpServletResponse response) throws IOException
{
PrintWriter out = response.getWriter();
Enumeration < String > e = request.getParameterNames();
String parameterName, parameterValue;
while (e.hasMoreElements())
{
parameterName = e.nextElement();
parameterValue = request.getParameter(parameterName);
out.println(parameterName + " : " + parameterValue + " <br> " );
}
}
}