当前位置: 代码迷 >> Java Web开发 >> 为啥必须是http://localhost:8080/HelloWorld/HelloWorld才能访问Servlet的HelloWorld
  详细解决方案

为啥必须是http://localhost:8080/HelloWorld/HelloWorld才能访问Servlet的HelloWorld

热度:7028   发布时间:2016-04-12 12:00:41.0
为什么必须是http://localhost:8080/HelloWorld/HelloWorld才能访问Servlet的HelloWorld?
我用Eclipse写了一个HelloWorld,运行时发现地址栏显示http://localhost:8080/HelloWorld/HelloWorld,这个/HelloWorld有重复,只有这个地址能运行,如果去掉一个/HelloWorld就不能运行了,怎么回事?我想去掉这个重复的还能运行应该怎么做?代码如下:

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 * Servlet implementation class HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
private String message;

public void init() throws ServletException
{
message = "Hello world!";
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");
}

public void destroy()
{

}
}

------解决思路----------------------
第一个helloworld是你的项目名
------解决思路----------------------
引用:
自己设置的@WebServlet("/HelloWorld"), 删了就行了

这个就是完全照着做的原因,注解的含义都没明白 
建议好好买一本spring 的入门书看。
------解决思路----------------------
第一个helloword是你项目名 第二个是你@WebServlet("/HelloWorld")上的,你把项目名改为hello url就是http://localhost:8080/Hello/HelloWorld。
------解决思路----------------------
专业点回答:第一个helloworld是你的context path
一般回答,项目名称改一下都不知道吗/?
------解决思路----------------------
不要被名字给忽悠到了,是你自己取的而已,项目名和action 名都用的一样
  相关解决方案