大家好,我正在写一个局域网内的搜索引擎,由于是初学者,还是有很多东西不懂 以下是我的代码
//从index.jsp转向一个Servlet
<form name="SearchBar" action="SearchBeginProcess" method="post"> 
    <input type="text" name="keys" size="128"> 
//以下是SearchBeginProcess.javapublic class SearchBeginProcess extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
           doPost(request, response);
           }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
response.setContentType("text/html; charset=gb2312");
String QueryString=request.getParameter("keys");
URL url=new URL("http://127.0.0.1:8080/Mephisto/Test?" +
     "querystring="+QueryString);
     System.out.println(url);
  //这里用了HttpURLConnection的方式将搜索关键字传到了Test这个Servlet
     HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//      conn.getInputStream();
     BufferedReader reader = new BufferedReader(new InputStreamReader(
                conn.getInputStream(),"gb2312"));
        int code = conn.getResponseCode();
        System.out.println(code);         System.out.println("=============================");
        System.out.println("Contents of get request");
        System.out.println("=============================");
        String lines;
        
        while ((lines = reader.readLine()) != null) {
            System.out.println(lines);
       }
        reader.close();
        // 断开连接
        conn.disconnect();
        System.out.println("=============================");
        System.out.println("Contents of get request ends");
        System.out.println("=============================");
    
//这里将传输信息写入Tomcat控制台


}
}
//Test.java
public class Test extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
           doPost(request, response);

           }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
response.setCharacterEncoding("gb2312");
          request.getRequestDispatcher("/Show.jsp").forward(request, response);
}
}
部署到Tomcat后启动Tomcat无任何问题,但是进入index.jsp 输入搜索关键字之后无法正确显示Show.jsp的结果,
在网页中显示一片空白
控制台输出的代码:
http://127.0.0.1:8080/Mephisto/Test?querystring=qwerty
200
=============================
Contents of get request
=============================<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Test page</title>
</head>
<body>
<h1 align="center">Welcome!</h1>
<h1>qwerty</h1>
</body>
</html>
=============================
Contents of get request ends
=============================

解决方案 »

  1.   

    看一下你JSP路径在什么地方,最好直接写/工程名/jsp名,如果jsp不是在包webwoot下,而是在webwoot/包名/jsp名,那么应该写/工程名/包名/jsp名!
      

  2.   

    你的SearchBeginProcess.java 只请求了没返回,而Test.java 的跳转页面信息已经被SearchBeginProcess.java读取了,不会根据你Test.java里面的跳转而到该页。所以正确的写法应该是在SearchBeginProcess.java的        
             String lines; 
            while ((lines = reader.readLine()) != null) { 
                System.out.println(lines); 
          } 
    位置改为
             String lines; 
    PrintWriter out = response.getWriter();
            while ((lines = reader.readLine()) != null) {             out.println(lines); 
          } 
    就显示你需要的页面了