我新建了一个servlet,可是运行不起
我的平台环境:myeclipse10+tamcat5.5LoginServlet.java
package com.lin.servlet;
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 LoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.lin.servlet.LoginServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>com.lin.servlet.test</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的项目
我在浏览器地址栏输入:http://localhost:8080/shop/login
jsp servlet java --------------------编程问答-------------------- 把工程下面的.settings/org.eclipse.wst.common.component 文件内容贴出来看看. --------------------编程问答-------------------- 需要项目的tomcat配置文件server.xml --------------------编程问答-------------------- 请问怎么配置serveer.xml? --------------------编程问答-------------------- resp.sendRedirect("xxxx.html"); --------------------编程问答-------------------- 或者在.settings/org.eclipse.wst.common.component 文件内容中的context-root的value改成/shop.
就是
<property name="context-root" value="/shop"/> --------------------编程问答--------------------
工程下面的.settings/org.eclipse.wst.common.component
--------------------编程问答-------------------- 你跳转的页面没有设置吧。 response.sendRedirect("xxxx.jsp");
<?xml version="1.0" encoding="UTF-8"?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="shop">
<wb-resource deploy-path="/" source-path="/WebRoot"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property name="context-root" value="/shop"/>
<property name="java-output-path" value="/shop/WebRoot/WEB-INF/classes"/>
</wb-module>
</project-modules>
404页面未找到。 --------------------编程问答-------------------- .settings/org.eclipse.wst.common.component 文件
就是
<property name="context-root" value="/shop"/>,出错还会在哪里呢? --------------------编程问答-------------------- 不需要跳转页面啊,servlet直接输出html --------------------编程问答-------------------- 先访问localhost:8080/login试试
不行再把/login改成login
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>login</url-pattern>
</servlet-mapping>
访问localhost:8080/shop/login试试 --------------------编程问答-------------------- 你先把你那些代码注释掉 然后 直接跳转看看有没有问题 ,怀疑部署的路径问题。 --------------------编程问答-------------------- 两种方式都试了,不行,还是404错误 --------------------编程问答-------------------- 再不行看看<servlet-class>com.lin.servlet.LoginServlet</servlet-class> 路径是不是对的.
myeclipse应该按住ctrl键可以直接点进去.
或者看看有没有部署成功.
一般在workspace的下面的.metadata\.plugins\org.eclipse.wst.server.core\tmp0 (如果是1个tomcat就从0开始).
看看下面的wtpwebapps文件夹里面有没有你的项目. --------------------编程问答-------------------- 不行,还是404错误 --------------------编程问答-------------------- 404! --------------------编程问答-------------------- 404! --------------------编程问答-------------------- <property name="context-root" value="shop"/> --------------------编程问答-------------------- E:\20131402039\jsp\.metadata\.plugins\org.eclipse.wst.server.core文件夹下面是空的是怎么回事? --------------------编程问答-------------------- 你直接考到tomcat 文件下,start.bat 看看。
--------------------编程问答-------------------- 或者 http://localhost:8080/ 在界面部署。 --------------------编程问答--------------------
那我也不知道了, 在你发的配置文件当中没发现错误的地方.
404是找不到资源时发生的错误.
我没用过MyEclipse可能部署路径不一样.
http://blog.csdn.net/bearray123/article/details/6868661
或者网上找myeclipse tomcat部署路径. --------------------编程问答-------------------- 我是刚刚学JSP的,这个“start.bat”是什么? --------------------编程问答-------------------- 界面怎么部署啊? --------------------编程问答--------------------
我之前有导入一个项目,它的servlet就可以运行起
TestServlet
package com.wy.servlet;
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 TestServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public TestServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("['6s','7s','8s']");
out.flush();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
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/j2ee" xmlns:javaee="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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<servlet>
<servlet-name>UserInfoServlet</servlet-name>
<servlet-class>com.wy.servlet.UserInfoServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>PhotoServlet</servlet-name>
<servlet-class>com.wy.servlet.PhotoServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>com.wy.servlet.TestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>SpecialServelet</servlet-name>
<servlet-class>com.wy.servlet.SpecialServelet</servlet-class>
</servlet>
<servlet>
<servlet-name>adminManger</servlet-name>
<servlet-class>com.wy.servlet.adminManger</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserInfoServlet</servlet-name>
<url-pattern>/UserInfoServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SpecialServelet</servlet-name>
<url-pattern>/SpecialServelet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>PhotoServlet</servlet-name>
<url-pattern>/photoServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>adminManger</servlet-name>
<url-pattern>/adminManger</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
导入的项目结构
我在浏览器中输入:http://localhost:8080/13/TestServlet运行正常,这是怎么回事? --------------------编程问答-------------------- 你JDK或jar包错了,项目都包大红叹号了
补充:Java , Web 开发