servlet不加载jsp

时间:2013-11-18 21:08:12

标签: java jsp servlets web

我正在尝试将jsp映射到servlet这样的

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>HockeyNightWeb</display-name>


    <servlet>
        <servlet-name>DefaultServlet</servlet-name>
        <servlet-class>DefaultServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DefaultServlet</servlet-name>
        <url-pattern>/DefaultServlet</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>DefaultServlet</servlet-name>
        <url-pattern>/login.jsp</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>DefaultServlet</servlet-name>
        <url-pattern>/DefaultServlet/login.jsp</url-pattern>
    </servlet-mapping>

     <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>


</web-app>

所以默认页面是login.jsp,当我没有将它映射到servlet时,页面显示出来。但是当它被映射时,它会进入servlet但是login.jsp没有显示,并且url保持http://localhost:8080/HockeyNightWeb/

当我调试时,它会进入doGet然后没有任何反应

这是servlet

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Servlet implementation class DefaultServlet
 */

public class DefaultServlet extends HttpServlet 
{
    private static final long serialVersionUID = 1L;
    private MatchHandler matchHandler;
    private RequestDispatcher jsp;
    private ParisHandler parisHandler;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DefaultServlet() 
    {
        super();
        matchHandler = new MatchHandler();
        parisHandler = new ParisHandler();
    }



    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        System.out.println("doGet");
        processRequest(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        System.out.println("doPost");
        processRequest(request, response);
    }



    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. get received JSON data from request
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String json = null;
        try 
        {
            /*json = matchHandler.GetListeMatchJSON();
            System.out.println(json);

            String reponse =  parisHandler.PostParis("Miaw", "Montreal", 5, "1");
            System.out.println(reponse);*/
        } 
        catch (ClassNotFoundException e) 
        {
            e.printStackTrace();
        }
        //response.getWriter().write(arg0);
        //response.sendRedirect("http://localhost:8080/HockeyNightWeb/index.jsp");
    }



}

有谁知道为什么? 谢谢

2 个答案:

答案 0 :(得分:1)

注意:您指的是具有相同servlet-name的不同URL!

仅将此用作您的web.xml条目

<display-name>HockeyNightWeb</display-name>
   <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
   </welcome-file-list>

    <servlet>
        <servlet-name>DefaultServlet</servlet-name>
        <servlet-class>DefaultServlet</servlet-class>
    </servlet>
</web-app>

答案 1 :(得分:0)

您必须将请求从servlet分派到所需的jsp页面。以下是“如何从servlet向jsp发送请求”的答案

redirect jsp from servlet RequestDispatcher

阅读JEE HttpServletRequest页面,了解有关如何知道何时发送的信息,以及提示:读取getRequestURI。

这是Good servlet and JSP tutorial site : Core Servlets and JSP。阅读“Intermediate Servlets&amp; JSP”和“Advanced Servlets&amp; JSP”文章。