日期:2014-05-17  浏览次数:20710 次

关于jsp页面中动态引入一个servlet的乱码问题
我在一个jsp页面中动态的引入了一个servlet,结果在jsp中显示的都是servlet的代码
现在附上源码
这个是那个jsp源码
<%@ page contentType="text/html;charset=GBK" %>
<%@ page language="java" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <body>
  <table width="98%" align="center" cellpadding=0 cellSpacing=1 class=i_table style="table-layout:fixed;word-wrap:break-word ">
<tr>
<td height="80" vAlign="top"><img src="images/logo2.jpg" width="260" height="80"></td>
</tr>
<jsp:include page="/Tophead.java"/>
</table>

  </body>
</html>


这个是引入的servlet源码

package mybbs;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Tophead extends HttpServlet{
PrintWriter out;
ResultSet rs;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException{
response.setContentType("text/html;charset=gbk");
out=response.getWriter();
HttpSession session=request.getSession();
//判断用户是否已经登录,进一步获取用户权限,显示登录后的导航条
out.print("<tr><td height='23' vAlign=top background='images/head.jpg'>");
out.print("<a href='default.jsp'>论坛首页&nbsp;&nbsp;|&nbsp;</a>");
out.print((String)session.getAttribute("nickname")+"|&nbsp;");
out.print("<a href='login.do'>重新登录</a>|&nbsp;");
out.print("<a href='user_info.do'>查看资料</a>|&nbsp;");
out.print("<a href='modifyinfo.do'>修改资料</a>|&nbsp;");
out.print("<a href='modifypass.do'>修改密码</a>|&nbsp;");
out.print("<a href='search.do?nickname="+session.getAttribute("nickname")+"'>搜索</a>|&nbsp;");
out.print("<a href='Login.do?action=out'>退出</a>|&nbsp;");

public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException{
doPost(request,response);
}
}
}


------解决方案--------------------
<jsp:include page="/Tophead.java"/>

这里应该是Servlet的Mapping地址,不是这个java类,你以前发布过Servlet么?



另外,不推荐用JSP去Include Servlet;应该采用MVC架构,先用Servlet准备好所有数据,然后转到JSP负责进行显示。
------解决方案--------------------
是这样引用的
<%@ page import="servlet文件名"%>
注意把你的servlet文件的路径写正确
------解决方案--------------------
一楼正解,