Eclipse提示从localhost下载servlet

时间:2013-05-23 03:41:00

标签: java servlets

每次运行它时,我都被要求从本地主机下载正在运行的servlet(CmdServlet),并将url更改为 http:// localhost:8080 / CommandWeb / CmdServlet?cmd = weather& loc = 12780913 我正在使用Tomcat6

CmdServlet

package controller;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import Command.Command;
import Command.WeatherCommand;

/**
 * Servlet implementation class CmdServlet
 */
public class CmdServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    private HashMap<String, Command> commands;
    private String error = "error.jsp";
    private String jspdir = "/";



public void init(ServletConfig config) throws ServletException {
    super.init(config);
    initCommands();
}

private void initCommands() {
    commands = new HashMap<String, Command>();
    commands.put("weather",new WeatherCommand());

}

public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String next = "";

    try{
        Command cmd = lookupCommand(request.getParameter("cmd"));
        cmd.execute(request,response);
        System.out.println("CmdServlet:cmd = " + cmd + ", zip = " + next);
    } catch(Exception e) {
        e.printStackTrace();
    }
}

private Command lookupCommand(String key) {
    return commands.get(key);
}

WeatherCommand

package Command;

import java.io.IOException;
import java.io.PrintWriter;

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



/**
 * Servlet implementation class WeatherCommand
 */
public class WeatherCommand extends HttpServlet implements Command{
private static final long serialVersionUID = 1L;

/**
 * Default constructor. 
 */
public WeatherCommand() {
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("test/html");
    PrintWriter pw = response.getWriter();

    String zip = request.getParameter("loc");
    System.out.println(zip);
    WeatherWrapper w = null;
    try {
        w = Weather.getWeather(zip);
        pw.println(w.getCondition());
    } catch (Exception e) {
            // TODO Auto-generated catch block
        response.sendError(0,"Not A Location");
    }
    pw.close();
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

@Override
public String execute(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    doGet(request,response);
    return "";
}

    }

的web.xml

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-    app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>CommandWeb</display-name>
<welcome-file-list>
<welcome-file>Command.html</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>CmdServlet</display-name>
<servlet-name>CmdServlet</servlet-name>
<servlet-class>controller.CmdServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CmdServlet</servlet-name>
<url-pattern>/CmdServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>WeatherCommand</display-name>
<servlet-name>WeatherCommand</servlet-name>
<servlet-class>Command.WeatherCommand</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WeatherCommand</servlet-name>
<url-pattern>/WeatherCommand</url-pattern>
</servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:5)

您在content-type方法上发送了“非标准”doGet()标头。由于浏览器无法识别它,它将考虑二进制类型并提示用户下载。

更改您的回复内容类型。

自:

response.setContentType("test/html");

要:

response.setContentType("text/html");