TOMCAT 总是找不到文件..求解The requested resource (/web02/addEmp.html) is not available

yangzj1992 2013-08-27 10:20:56
刚起步,做的是一个非常简单的连接数据库
在运行的过程中出现The requested resource (/web02/addEmp.html) is not available.

下面是我的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">
<servlet>
<servlet-name>addEmpServlet</servlet-name>
<servlet-class>web.AddEmpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>addEmpServlet</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>

<error-page>
<!--方式1-->
<error-code>500</error-code>
<!--方式2
<exception-type>javax.servlet.ServletException
</exception-type>
-->
<location>/error.html</location>
</error-page>
</web-app>



两个HTML文件:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>addEmp</title>
</head>

<body style="font-size: 30px;">
<form action="add" method="post;">
<fieldset>
<legend>添加雇员</legend>
姓名:<input name="name"/><br/>
薪水:<input salary="salary"/><br/>
年龄:<input age="age"/><br/>
<input type="submit" value="确认";/>
</fieldset>
</form>
</body>
</html>



<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>sorry.</title>
</head>

<body style="font-size: 30px;color: red;">
发生了系统错误,请歇会。
<a href="addEmp.html">try again!~</a>
</body>
</html>


以及主程序如下:

package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import java.sql.Connection;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
import com.mysql.jdbc.PreparedStatement;
import com.sun.net.httpserver.HttpServer;
public class AddEmpServlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//这行代码要放在getparameter()执行之前。
request.setCharacterEncoding("utf-8");

String name =request.getParameter("name");
double salary=Double.parseDouble(request.getParameter("salary"));
int age=Integer.parseInt(request.getParameter("age"));
System.out.println("name:"+name);
System.out.println("salary:"+salary);
System.out.println("age"+age);

//访问数据库
Connection conn=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/jd1109db2","root","root");
String sql = null;
java.sql.PreparedStatement prep=conn.prepareStatement(sql);
prep.setString(1, name);
prep.setDouble(2, salary);
prep.setInt(3, age);
prep.executeUpdate();

response.setContentType("text/html;charset=utf-8");
PrintWriter out =response.getWriter();
out.println("添加雇员成功");
out.close();
}catch (Exception e) {
/*异常catch之后,
* 要区分对待不同的异常类型。
*/

/*对于系统异常
* (不是由于程序本身的问题引起的异常,
* 而是由于工作环境,如网络问题,数据库问题等产生的异常
* 如数据库未启动,
* 使用DRIVERMANAGER.GETCONNECTION()是无论如何
* 也得不到连接的,会报异常,这种异常就是系统异常。
* 程序无能为力,但是必须告诉用户。
* 所以还须提示用户
*/

//step1 先记录日志
e.printStackTrace();
//step2 抛出
throw new ServletException(e);
}finally{
if(conn!=null){
try{
conn.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}

}
}



以上是全部代码..好不容易不报错了,TOMCAT居然打不开..调了一上午真的泪奔了..
求好心人看看为什么会这样,谢谢了..
...全文
1218 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
2013-08-27
  • 打赏
  • 举报
回复
The requested resource (/web02/addEmp.html) is not available. 就这么些代码么。 没有调用 “/web02/addEmp.html”的代码???
yangzj1992 2013-08-27
  • 打赏
  • 举报
回复
引用 1 楼 ch656409110 的回复:
The requested resource (/web02/addEmp.html) is not available.

指定的访问路径不对,,你是怎么写的,发布的时候 虚拟发布还是把项目直接放到webapps中的,,

如图..
发布那个不太懂什么意思..


  • 打赏
  • 举报
回复
The requested resource (/web02/addEmp.html) is not available. 指定的访问路径不对,,你是怎么写的,发布的时候 虚拟发布还是把项目直接放到webapps中的,,
yangzj1992 2013-08-27
  • 打赏
  • 举报
回复
费了番心思,进到了TOMCAT7的管理界面:
我看了你们的回复后,发现TOMCAT里面确实没有WEB02这个项目,搜了一下,我把WEB02复制到了webapp里.

然后管理界面里也有了,但还是如之前4楼第二个图的情景。。

学校机房CSDN居然没法上传图片...
图片大家麻烦得挪步看下了...:http://xiangce.baidu.com/picture/detail/d4a86700a41f0831288ff6a013c232e7bebc6457


谢谢大家的回复
lgh1117 2013-08-27
  • 打赏
  • 举报
回复
1、你提供的程序里面,界面标签就有问题,form的action值是post,而你写成“post;”; 2、资源找不到,查看你项目在tomcat中否发布成功; 3、如果在webapps包中有你的web02项目(文件夹名称),则查看应用程序是否发布成功; 4、查看应用程序是否发布成功,登录tomcat管理界面即可; 5、servlet的书写不要重写里面的service方法,而是根据自己的需要重写doPost或者doGet等相应的服务方法;
  • 打赏
  • 举报
回复
引用 2 楼 yangzj1992 的回复:
[quote=引用 1 楼 ch656409110 的回复:] The requested resource (/web02/addEmp.html) is not available. 指定的访问路径不对,,你是怎么写的,发布的时候 虚拟发布还是把项目直接放到webapps中的,,
如图.. 发布那个不太懂什么意思.. [/quote] localhost:8080 应该可以访问对吧。。??那就是你现在发布的项目没有发布成功。。你去你tomcat webapps下面找找看,,肯定没有的。。 http://blog.csdn.net/chenghui0317/article/details/9417949

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧