我正在为天气预报开发Android应用程序。目前我已经在servlet中编写了服务器端代码。 我的网络服务器是Apache Tomcat。如何在Web服务器上部署我的servlet?部署servlet的步骤是什么。请提前告诉你。
package com.example.WeatherDetails;
import com.example.Info.INFORMATION;
import com.example.Info.WeatherInformation;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class WeatherDetails extends HttpServlet
{
public WeatherDetails()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doPost(request,response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String LAT = request.getParameter("LAT");
String LONGITUDE = request.getParameter("LONGITUDE");
PrintWriter out = response.getWriter();
//get list of countries
WeatherInformation WeatherInformation = new WeatherInformation();
ArrayList<INFORMATION> WeatherInfo = WeatherInformation.getList(LAT,LONGITUDE);
Gson gson = new Gson();
JsonArray arrayObj=new JsonArray();
for(int i=0;i<WeatherInfo.size();i++)
{
INFORMATION information = WeatherInfo.get(i);
JsonElement productObj = gson.toJsonTree(information);
arrayObj.add(productObj);
}
//create a new JSON object
JsonObject myObj = new JsonObject();
//add property as success
myObj.addProperty("success", true);
//add the countryList object
myObj.add("WeatherInfo", arrayObj);
//convert the JSON to string and send back
out.println(myObj.toString());
out.close();
}
}
答案 0 :(得分:0)
您必须在web.xml文件中提供servlet映射
作为
<servlet>
<servlet-name>servletname</servlet-name>
<servlet-class>packagename.classname</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletname</servlet-name>
<url-pattern>/servletname</url-pattern>
//这是用于访问servlet的servlet根目录
之后将其部署在服务器上并使用url:localhost:8080/projectname/servletroot