myeclipse jsp servlet 部署
但是我将WebRoot下的所有东西复制到工程根目录下 就可以通过浏览器打开该servlet
跪求解释 展开
没有啊,我刚刚特地试了下,可以的。 你的是不是web.xml或者服务器conf路径没配置好。
MyEclipse 新建WebProject (HelloWorld)
//HelloWorld.java
package org.penglx.test;
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{
// 表示处理get请求
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws
IOException,ServletException{
PrintWriter out = resp.getWriter() ;
out.println("<HTML>") ;
out.println("<HEAD>") ;
out.println("<TITLE>THE FIRST SERVLET</TITLE>") ;
out.println("</HEAD>") ;
out.println("<BODY>") ;
out.println("<H1>Hello World!!!</H1>") ;
out.println("</BODY>") ;
out.println("</HTML>") ;
out.close() ;
}
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws
IOException,ServletException{
this.doGet(req,resp) ;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello");
}
}
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.penglx.test.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
//我用的是RESIN服务器,你也可以用tomcat
//resin.conf
//前面省略
<web-app id="" document-directory="D:\WorkSpace\HelloWorld\WebRoot"/>
//后面的也省略
//测试:启动服务器,打开IE,访问 http://localhost:8080/hello
//结果显示:Hello World!!!
指的是你的servlet所放的目录结构,记得改成你的啊
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>cn.com.control.ActionServlet</servlet-class
</servlet>
<!-- mapping -->
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>/actionServlet</url-pattern>
</servlet-mapping>
web.xml不是自动生成吗 需要手动修改吗