我一直在运行 (CmdServlet) 并且每次更改URL为 http:// localhost:8080/CommandWeb/CmdServlet?cmd=weather&loc=12780913 时,都会提示我下载正在运行的servlet。我正在使用Tomcat6。
该指令Servlet(CmdServlet)。
该指令Servlet(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 "";
}
}
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>