jsp向servlet传递参数的两种情况及中文乱码处理

第一种方法是通过get方式传递参数:

parameter.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

	
		这是我的第一个JSP页面
	
	
	
	
	paratest
	

 ParaServlet.java的代码如下:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class ParaServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{
		String str = request.getParameter("a");
		String str1 = new String(str.getBytes("ISO-8859-1"),"GBK");

		System.out.println(str1);

	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{
	}

}

 

get方式通过

String str1 = new String(str.getBytes("ISO-8859-1"),"GBK");
来转码。

第二种是通过post方式传递参数:

parameter2.jsp代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

	
		这是我的第一个JSP页面
	
	
	
	
	

 ParaServlet.java的代码如下:

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
public class ParaServlet extends HttpServlet
{
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{


	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{
		request.setCharacterEncoding("GBK");
		String str = request.getParameter("test");
		System.out.println(str);

}
}

 

post方式是通过request.setCharacterEncoding("GBK"); 来转码的

如果想要传递中文参数  get方式比较少用  因为get方式麻烦

 

你可能感兴趣的:(JSP学习笔记)