[英]Getting Server Error while calling Servlet from Java Application
Here is my Java Class by which I am calling simple servlet and passing the data, I am 这是我的Java类,通过它我可以调用简单的servlet并传递数据,
using URL and HttpURlConnection class.What should be path of url for the 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();
}
}
}
Here is the servlet code, I am receiving the object from the java code and displaying 这是servlet代码,我正在从Java代码接收对象并显示
it on the console. 它在控制台上。
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);
}
}
My web.xml is like this 我的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>
When im trying to run the java code on server ie Apache server, I'm getting 当我试图在服务器即Apache服务器上运行Java代码时,我正在
error HTTP Status 404 i am not able to find why i am getting this server error My code is all about invoking the servlet from java application 错误HTTP状态404我无法找到为什么得到此服务器错误我的代码全部是关于从Java应用程序调用servlet的
please help me guys .
So to answer your question : "What should be path of url for the servlet?" 因此,回答您的问题:“该servlet的URL路径应该是什么?” I think you should know the following :
我认为您应该了解以下内容:
The URL should be something like : 该网址应类似于:
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.
So having that said the URL for you should be : 因此,您的网址应该为:
http://localhost:8080/NameOfTheContext/PushServlet
If it's unclear where you can find the NameOfTheContext tell us how did you deployed the servlet , so we may help you. 如果不清楚在哪里可以找到NameOfTheContext,请告诉我们您如何部署servlet,因此我们可以为您提供帮助。
Later Edit : the name of the context should be : JavaToServlet because that's the name of your .WAR archive . 以后的编辑 :上下文的名称应该是: JavaToServlet,因为那是您的.WAR档案的名称。 So your URL should be :
http://localhost:9865/JavaToServlet/PushServlet
因此,您的网址应为:
http://localhost:9865/JavaToServlet/PushServlet
From the reference on web for 404 Error WiKi 从网上的404错误WiKi参考
The web site hosting server will typically generate a "404 Not Found" web page when a user attempts to follow a broken or dead link;
当用户尝试访问断开或无效的链接时,网站托管服务器通常会生成“ 404 Not Found”网页。
You can more about 404 error from below link 您可以从下面的链接中进一步了解404错误
404 Not Found Error (What It Is and How To Fix It) . 404 Not Found错误(这是什么以及如何解决) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.