在eclipse中Servlet的排坑
程序员文章站
2022-05-23 14:00:17
...
1.结果
建了一个web project ,然后servlet类终于在web.xml映射中被找到,在浏览器中输入
http://localhost:8080/MyServlet2/myserv?name=liuyan&age=22 也终于有了反应
(一开始是打死也找不到web.xml中映射的servlet类,没有理由)
2.简单的开始
在web-inf的lib中要引入相关的servlet包,包在下图中可以找到:
然后在src下建一个包Test
然后新建一个servlet类myserv.java,代码如下:
public class myserv extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public myserv() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name + "," + age );
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3.配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyServlet2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>myserv</display-name>
<servlet-name>myserv</servlet-name>
<servlet-class>Test.myserv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myserv</servlet-name>
<url-pattern>/myserv</url-pattern>
</servlet-mapping>
</web-app>
4.问题开始出现了
一般这个时候都是restart tomcate 然后去浏览器输入:
http://localhost:8080/MyServlet2/myserv?name=liuyan&age=22
看会不会有网页显示,并在eclipse中有打印信息。
但是在这个地方被坑了很久,打印信息就是一直报错,没有任何理由(说没有找到)
<servlet-class>Test.myserv</servlet-class>
Test.myserv
你说奇不奇怪!
5.解决办法
1.重建(新建)web项目,改变A中的值,B中选择javaserver开头的。
2.点击eclipse菜单栏的project,然后clean这个web工程,再重启tomcate.(实测有效)
3.右击红框处,然后monitoring把8080打勾,已经打勾的无视这个解决办法
6.安心的在浏览器中输入,并观察eclipse中的打印结果
7.一个servlet例子
myserv.java
public class myserv extends HttpServlet { ////每次代码改变,都要clean一下工程,再重启服务器
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public myserv() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name + "," + age );
response.getWriter().append("Served at: ").append(request.getContextPath()); //servlet返回数据
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
/*//post 这里要新一个servlet,测试用,写在了一起
String name = request.getParameter("name");
String age = request.getParameter("age");
System.out.println(name + "," + age );*/
/*//poststring 这里要新一个servlet,测试用,写在了一起
DataInputStream ds = new DataInputStream(request.getInputStream());
//ServletInputStream is = request.getInputStream();
StringBuilder sb = new StringBuilder();
int len = 0;
byte[] buf = new byte[1024];
while((len = ds.read(buf)) != -1){
sb.append(new String(buf , 0 , len , "utf-8")); //获取string并存到sb当中
}
System.out.println(sb.toString());*/
/*//post_file 这里要新一个servlet,测试用,写在了一起
DataInputStream ds = new DataInputStream(request.getInputStream());
//ServletInputStream is = request.getInputStream();
//定义file存放的位置
File file = new File( "C:\\Users\\liuyan\\Desktop" , "liuyan.jpg");
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while((len = ds.read(buf)) != -1){
fos.write(buf, 0, len); //获取string并存到file当中
}
fos.flush();
fos.close();
System.out.println("file ok");*/
//post file and params 这里要新一个servlet,测试用,写在了一起
//参数获得
//不知道
/*//上传文件
File file = new File( "C:\\Users\\liuyan\\Desktop" , "imagename.jpg");
DataInputStream ds = new DataInputStream(request.getInputStream());
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] buf = new byte[1024];
while((len = ds.read(buf)) != -1){
fos.write(buf, 0, len); //获取string并存到file当中
}
fos.flush();
fos.close();
System.out.println("file ok");*/
response.getWriter().append("Served at: ").append(request.getContextPath());
}
}
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyServlet2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet> //如果有多个servlet也要配置在这里
<description></description>
<display-name>myserv</display-name>
<servlet-name>myserv</servlet-name>
<servlet-class>Test.myserv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myserv</servlet-name>
<url-pattern>/myserv</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>myserv</display-name>
<servlet-name>myserv</servlet-name>
<servlet-class>Test.myserv</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myserv</servlet-name>
<url-pattern>/myserv</url-pattern>
</servlet-mapping>
</web-app>
上一篇: Three.js骨骼动画(SkinnedMesh)
下一篇: Three.js入门学习
推荐阅读
-
关于Oracle的在Eclipse中操作的命令语句
-
在eclipse中编写HDFS的Java程序
-
在Eclipse中更改maven项目名的方法
-
详谈Servlet和Filter的区别以及两者在Struts2和Springmvc中的应用
-
解析web.xml中在Servlet中获取context-param和init-param内的参数
-
解析web.xml中在Servlet中获取context-param和init-param内的参数
-
java,Android:在eclipse中的快捷键(经典收藏)
-
java,Android:在eclipse中的快捷键(经典收藏)
-
Python中使用遍历在列表中添加字典遇到的坑
-
关于在eclipse中添加windowbuilder插件的问题