gpt4 book ai didi

java - Eclipse提示从localhost下载servlet

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:49:53 24 4
gpt4 key购买 nike

每次运行时我都会被提示从本地主机下载正在运行的 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);
}

天气命令

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 "";
}

}

网络.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>

最佳答案

您在 doGet() 方法上发送“非标准”content-type header 。由于浏览器无法识别,会认为是二进制类型,提示用户下载。

更改您的响应内容类型。

来自:

response.setContentType("test/html");

收件人:

response.setContentType("text/html");

关于java - Eclipse提示从localhost下载servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16705284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com