新的servlet helloWorld

时间:2014-02-17 08:11:50

标签: java servlets

我是使用servlet的新手。我想运行一个简单的helloWorld servlet代码但是在运行它时,我收到404错误:请求的资源()不可用。

我的项目名称是“ServletPractice”。

我的java类是:

package one;

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;

public class HelloWorld extends HttpServlet {

private String message;

public void init(){
    message = "hello world";
}

public void doGet(HttpServletRequest request , HttpServletResponse response) throws IOException , ServletException{
    response.setContentType("text/html");

    PrintWriter out = response.getWriter();
    out.println("<h1>" + message + "</h1>");
}

public void destroy()
  {
      // do nothing.
  }

/**
 * @param args
 */
public static void main(String[] args) {
    // 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" 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_3_0.xsd" version="3.0">
<display-name>ServletPractice</display-name>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

<servlet>
<servlet-name>ServletPractice</servlet-name>
<servlet-class>ServletPractice</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ServletPractice</servlet-name>
<url-pattern>/ServletPractice</url-pattern>
</servlet-mapping>

</web-app>

错误:

HTTP Status 404 -

type: Status report

message:

description: The requested resource () is not available.
Apache Tomcat/7.0.16

我在地址栏中的地址是:     本地主机:9090 / ServletPractice / WEB-INF / web.xml中

请帮帮我。 thankes

3 个答案:

答案 0 :(得分:2)

您没有正确地在web.xml中映射您的servlet。您提供项目名称而不是Servlet名称,试试这个:

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>one.HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

确保在web.xml

中进行更改后重新启动了服务器

答案 1 :(得分:1)

您需要在Servlet文件中添加web.xml声明,例如:

<servlet>
    <servlet-name>HelloWorld</servlet-name>
    <servlet-class>one.HelloWorld</servlet-class>
</servlet>

然后添加映射到此Servlet

<servlet-mapping>
    <servlet-name>HelloWorld</servlet-name>
    <url-pattern>/HelloWorld</url-pattern> // or the pattern you want
</servlet-mapping>

答案 2 :(得分:0)

您需要在web.xml文件中为servlet添加servlet声明和url映射。          yourservletname     classnamewithpackage

<servlet-mapping>
<servlet-name>yourservletname</servlet-name>
<url-pattern>/yourservletname</url-pattern>