<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<% String message = "Hello World!"; String msg = (String)request.getAttribute("msg"); if(msg != null && !msg.equals("")) { message = msg; } %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP 'helloworld.jsp' starting page</title> </head> <body> <%=message %> <form action="servlet/HelloServlet" method="post"> <input type="text" name="name"/> <input type="submit" value="提交"/> </form> </body> </html>
package lee; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException{ String msg = null; String name = request.getParameter("name"); request.setCharacterEncoding("GBK"); if(name != null && !name.equals("")) { msg = "Hello " + name; } request.setAttribute("msg", msg); RequestDispatcher rd = request.getRequestDispatcher("/Hello/helloworld.jsp"); rd.forward(request, response); } }
小弟是Java web的初学者,今天学习时,写了一个简单的应用来测试servlet的页面转发功能,问题是: 当输入中文时,会出现乱码,不是编码的问题,这个我试过了,请问各位高手这是什么原因呢,怎么改呢?请指点指点,让大家见笑了。