[英]Getting Server Error while calling Servlet from Java Application
這是我的Java類,通過它我可以調用簡單的servlet並傳遞數據,
使用URL和HttpURlConnection類。servlet的url路徑應該是什么
public class TestJava
{
public static void main(String[] args)
{
try
{
URL url=new URL("http://localhost:9865/TestingServlet/PushServlet");
HttpURLConnection http_url =(HttpURLConnection)
url.openConnection();
http_url.setRequestMethod("POST");
http_url.setDoOutput(true);
http_url.setDoInput(true);
InputStream response = http_url.getInputStream();
System.out.println(" " +response);
ObjectOutputStream objOut = new
ObjectOutputStream(http_url.getOutputStream());
objOut.writeObject("hello");
objOut.flush();
objOut.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
這是servlet代碼,我正在從Java代碼接收對象並顯示
它在控制台上。
public class PushServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
try
{
System.out.println("HELLO This is servlet");
ObjectInputStream objIn = new
ObjectInputStream(request.getInputStream());
TestJava p = null;
p = (TestJava) objIn.readObject();
System.out.println("Servlet received p: "+p);
}
catch (Throwable e)
{
e.printStackTrace(System.out);
}
}
我的web.xml是這樣的
<welcome-file-list>
<welcome-file>
Customer_Servlet
</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>PushServlet</servlet-name>
<servlet-class>com.servlet.PushServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PushServlet</servlet-name>
<url-pattern>/PushServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>50</session-timeout>
</session-config>
</web-app>
當我試圖在服務器即Apache服務器上運行Java代碼時,我正在
錯誤HTTP狀態404我無法找到為什么得到此服務器錯誤我的代碼全部是關於從Java應用程序調用servlet的
please help me guys .
因此,回答您的問題:“該servlet的URL路徑應該是什么?” 我認為您應該了解以下內容:
該網址應類似於:
http://localhost:portNumber/nameOfTheContext/MappingOfTheServlet WHERE
portNumber = the port number of the server ( Apache has it by default to 80 but Apache Tomcat has it to 8080, so it's not 9865 unless you changed it from configuration file)
nameOfTheContext = the name of the folder/archive( with .WAR or .EAR extension) which you deployed to the server
MappingOfTheServlet = the mapping of the servlet which you put in web.xml. So if you have <url-pattern>/PushServlet</url-pattern> then the mapping is PushServlet.
因此,您的網址應該為:
http://localhost:8080/NameOfTheContext/PushServlet
如果不清楚在哪里可以找到NameOfTheContext,請告訴我們您如何部署servlet,因此我們可以為您提供幫助。
以后的編輯 :上下文的名稱應該是: JavaToServlet,因為那是您的.WAR檔案的名稱。 因此,您的網址應為: http://localhost:9865/JavaToServlet/PushServlet
從網上的404錯誤WiKi參考
當用戶嘗試訪問斷開或無效的鏈接時,網站托管服務器通常會生成“ 404 Not Found”網頁。
您可以從下面的鏈接中進一步了解404錯誤
404 Not Found錯誤(這是什么以及如何解決) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.