Jetty - Servlet无法找到 [英] Jetty - Servlet cannot be found

查看:394
本文介绍了Jetty - Servlet无法找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Servlet的新手,想要使用Eclipse的Jetty插件调用一个简单的Servlet。我可以调用index.html,但是当尝试访问Servlet时,我得到:


HTTP错误:404

访问/ ProjectServlet时遇到问题。原因:

NOT_FOUND


我认为我正确配置了所有文件,无法解释Jetty为什么返回此错误。感谢您帮助我!






索引.html

 <!DOCTYPE html> 
< html>
< head>
< meta charset =ISO-8859-1>
< title> TEST< / title>
< / head>
< body>
< h1> Servlet呼叫测试:< / h1>
< form action =/ ProjectServletmethod =GET>
< input type =textname =namemaxlength =20value =Nameonfocus =this.select()/>< br>
< input type =submitname =callservletvalue =Call Servlet。/>
< / form>
< / body>
< / html>






ProjectServlet.java

 包servlets; 


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/ **
* Servlet实现类ProjectServlet
* /
@WebServlet(description =使用项目列表返回JSON响应的Servlet,urlPatterns = { / ProjectServlet})
public class ProjectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/ **
*默认构造函数。
* /
public ProjectServlet(){
super();
}

/ **
* @see HttpServlet#doGet(HttpServletRequest请求,HttpServletResponse响应)
* /
protected void doGet(HttpServletRequest request, HttpServletResponse响应)throws ServletException,IOException {

//调用Servlet按钮
if(request.getParameter(callservlet)!= null){

响应。的setContentType( 应用程序/ JSON);

String name = request.getParameter(name);
String jsonexample =hi+ name;

response.getWriter()。write(jsonexample);
}
}

/ **
* @see HttpServlet#doPost(HttpServletRequest请求,HttpServletResponse响应)
* /
protected void doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
// TODO自动生成的方法存根
}

}






web.xml

 <?xml version =1.0encoding =UTF-8?> 
< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns =http://java.sun.com/xml/ns/javaeexsi :schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsdid =WebApp_IDversion =3.0 >
< display-name> Test< / display-name>
< welcome-file-list>
< welcome-file> index.html< / welcome-file>
< welcome-file> index.htm< / welcome-file>
< welcome-file> index.jsp< / welcome-file>
< welcome-file> default.html< / welcome-file>
< welcome-file> default.htm< / welcome-file>
< welcome-file> default.jsp< / welcome-file>
< / welcome-file-list>

< servlet>
< servlet-name> ProjectServlet< / servlet-name>
< servlet-class> servlets.ProjectServlet< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> ProjectServlet< / servlet-name>
< url-pattern> / ProjectServlet< / url-pattern>
< / servlet-mapping>
< / web-app>


解决方案

这应该如下所示添加

 < form action =$ {pageContext.servletContext.contextPath} / ProjectServlet
method =GET>

而不是

 code>< form action =/ ProjectServletmethod =GET> 


I am new to Servlets and wanted to call a simple Servlet by using Eclipse's Jetty plugin. I can call the index.html, but when trying to access the Servlet I get:

HTTP ERROR: 404
Problem accessing /ProjectServlet. Reason:
NOT_FOUND

I think I configured all my files correctly and cannot explain why Jetty returns this error.

Thanks for helping me!


index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>TEST</title>
</head>
<body>
    <h1>Servlet Call Test:</h1>
    <form action="/ProjectServlet" method="GET">
        <input type="text" name="name" maxlength="20" value="Name" onfocus="this.select()"/><br>
        <input type="submit" name="callservlet" value="Call Servlet."/>
    </form>
</body>
</html>


ProjectServlet.java

package servlets;


import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ProjectServlet
 */
@WebServlet(description = "Servlet to return JSON response with project list", urlPatterns = { "/ProjectServlet" })
public class ProjectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ProjectServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //Call Servlet Button
        if (request.getParameter("callservlet") != null) {

            response.setContentType("application/json");

            String name = request.getParameter("name"); 
            String jsonexample = "hi " + name; 

            response.getWriter().write(jsonexample);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}


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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>ProjectServlet</servlet-name>
    <servlet-class>servlets.ProjectServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ProjectServlet</servlet-name>
    <url-pattern>/ProjectServlet</url-pattern>
  </servlet-mapping>
</web-app>

解决方案

This should be as shown below to add the context path before Servlet url.

<form action="${pageContext.servletContext.contextPath}/ProjectServlet" 
         method="GET">

instead of

<form action="/ProjectServlet" method="GET">

这篇关于Jetty - Servlet无法找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆