怎么解决request.getRequestDispatcher("Show.jsp").forward(request, response)这个跳转时中文无法显示
servlet:request.setCharacterEncoding("gb2312");response.setContentType("text/html;cha...
servlet:
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
String name=request.getParameter("username");
String password=request.getParameter("pass");
try {
Context ct=new InitialContext();
DataSource ds=(DataSource) ct.lookup("java:comp/env/jdbc/students");
Connection conn=ds.getConnection();
ps=conn.prepareStatement("select * from student");
rs=ps.executeQuery();
while(rs.next()){
if((name.equals(rs.getString(2)))&&(password.equals(rs.getString(1)))){
Bean bean=new Bean();
bean.setName(name);
bean.setId(password);
request.setAttribute("bean", bean);
request.getRequestDispatcher("Show.jsp").forward(request, response);
}
else
response.sendRedirect("Index.html");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsp:
<%@ page language="java" import="java.util.*,Javabean.Bean" pageEncoding="gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<jsp:useBean id="bean" class="Javabean.Bean" scope="request" >
</jsp:useBean>
<p>呵呵</p>
<%=bean.getName() %>
<%=bean.getId() %>
</body>
</html> 展开
request.setCharacterEncoding("gb2312");
response.setContentType("text/html;charset=gb2312");
String name=request.getParameter("username");
String password=request.getParameter("pass");
try {
Context ct=new InitialContext();
DataSource ds=(DataSource) ct.lookup("java:comp/env/jdbc/students");
Connection conn=ds.getConnection();
ps=conn.prepareStatement("select * from student");
rs=ps.executeQuery();
while(rs.next()){
if((name.equals(rs.getString(2)))&&(password.equals(rs.getString(1)))){
Bean bean=new Bean();
bean.setName(name);
bean.setId(password);
request.setAttribute("bean", bean);
request.getRequestDispatcher("Show.jsp").forward(request, response);
}
else
response.sendRedirect("Index.html");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jsp:
<%@ page language="java" import="java.util.*,Javabean.Bean" pageEncoding="gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<jsp:useBean id="bean" class="Javabean.Bean" scope="request" >
</jsp:useBean>
<p>呵呵</p>
<%=bean.getName() %>
<%=bean.getId() %>
</body>
</html> 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询