直接打来jsp页面没有问题,servlet重定向到jsp页面出现乱码问题?望高手指点啊!
时间: 2011-10-27来源:开源中国
前景提要
HDC调试需求开发(15万预算),能者速来!>>>
我的servlet代码如下: package Servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.ResultSet; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import bean.Select; public class Login extends HttpServlet { /** * Constructor of the object. */ public Login() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * 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 { doPost(request,response); } /** * 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 { request.setCharacterEncoding("GBK"); response.setCharacterEncoding("GBK"); // PrintWriter out=response.getWriter(); response.setContentType("text/html;charset=GBK");//确定MIME类型 String errMsg=""; RequestDispatcher rd; String sno=request.getParameter("sno"); String pass=request.getParameter("password"); String sql="SELECT * FROM elec_user"; try{ Select sel=new Select("com.mysql.jdbc.Driver","jdbc:mysql://localhost:3306/elec","root","123"); sel.Connection(); ResultSet rs=sel.query(sql); while(rs.next()){ if(rs.getString("Password").equals(pass)){ HttpSession session=request.getSession(true); session.setAttribute("sno",sno); rd=request.getRequestDispatcher("/index.jsp"); rd.forward(request,response); } else { errMsg+="用户名或密码输入有误,请重新输入!"; } } rs.close(); sel.end(); } catch (Exception e) { e.printStackTrace(); } if(errMsg !=null && !errMsg.equals("")){ rd = request.getRequestDispatcher("/login.jsp"); request.setAttribute("err",errMsg); rd.forward(request,response); //response.sendRedirect("/Lib/login.jsp"); } } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init(ServletConfig config) throws ServletException { super.init(config); } } 下面是我的javabean代码:package bean; import java.sql.*; public class Select { private Connection conn; ResultSet rs; private String driver; private String url; private String username; private String password; public Select(){} public Select(String driver,String url,String username,String password){ this.driver=driver; this.url=url; this.username=username; this.password=password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getDriver() { return driver; } public void setDriver(String driver) { this.driver = driver; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Connection Connection() throws Exception{ //if(conn==null){ Class.forName(this.driver); conn=DriverManager.getConnection(url,username,password); //} return conn; } public ResultSet query(String sql) throws Exception{ Statement stmt=conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); return stmt.executeQuery(sql); } public void end(){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
下面是我的login.jsp页面代码: <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="servlet/Servlet.Login" method="post" > <p>学号: <input name="sno" type="text" maxlength="13" /> </p> <p>密码: <input name="password" type="password" /> </p> <input name="login" type="submit" value="登录" /> <input name="rewrite" type="reset" value="重填" /> </form> <%if(request.getAttribute("err")!=null) out.println(request.getAttribute("err"));%> </body> </html> 出现的问题是这样的,我用浏览器直接进入login.jsp页面可以正常显示,但提交后通过servlet重定向到login.jsp页面就是乱码了,在网上搜了很多的解决方案,但还是不行,还望请高手指教啊!

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行