Servlets servlet配置的混乱
在Myeclipse中,我创建了一个名为web1的web项目,并添加了一个名为servlet1的servlet,web.xml如下所示:Servlets servlet配置的混乱,servlets,Servlets,在Myeclipse中,我创建了一个名为web1的web项目,并添加了一个名为servlet1的servlet,web.xml如下所示: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
servlet1
servlet1
servlet1
/试验
index.jsp
但当我输入地址时:http://localhost:8080/web/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 servlet1 extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -6214906967399177511L;
/**
* Constructor of the object.
*/
public servlet1() {
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
}
}
import java.io.IOException;
导入java.io.PrintWriter;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
公共类servlet1扩展了HttpServlet{
/**
*
*/
私有静态最终长serialVersionUID=-621490696739177511L;
/**
*对象的构造函数。
*/
公共服务1(){
超级();
}
/**
*销毁servlet。
*/
公共空间销毁(){
super.destroy();//只需将“destroy”字符串放入日志
//把你的代码放在这里
}
/**
*servlet的doGet方法。
*
*当窗体的标记值方法等于get时,将调用此方法。
*
*@param request客户端向服务器发送的请求
*@param response服务器向客户端发送的响应
*@在发生错误时抛出ServletException
*@在发生错误时引发IOException
*/
public void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“”);
out.println(“Servlet”);
out.println(“”);
打印出来(“这是”);
out.print(this.getClass());
println(“,使用GET方法”);
out.println(“”);
out.println(“”);
out.flush();
out.close();
}
/**
*servlet的doPost方法。
*
*当窗体的标记值方法等于post时,将调用此方法。
*
*@param request客户端向服务器发送的请求
*@param response服务器向客户端发送的响应
*@在发生错误时抛出ServletException
*@在发生错误时引发IOException
*/
public void doPost(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
out.println(“”);
out.println(“”);
out.println(“Servlet”);
out.println(“”);
打印出来(“这是”);
out.print(this.getClass());
out.println(“,使用POST方法”);
out.println(“”);
out.println(“”);
out.flush();
out.close();
}
/**
*servlet的初始化。
*
*如果发生错误,@将抛出ServletException
*/
public void init()引发ServletException{
//把你的代码放在这里
}
}
和往常一样,您忘了说super.init(config)
您永远无法初始化servlet,因为您重写了方法,忘记调用父类的init
这就是为什么,如果您不了解内部结构,请不要覆盖任何内容,除非您确定自己在做什么。web服务器正在运行吗?向我们展示servlet代码。这是Myeclipse创建的默认servlet,我没有添加任何内容。我没有使用Myeclipse,所以我不知道默认servlet中包含什么内容。感谢您的回答,我将把代码放在问题中。我已解决了问题。我犯了一个错误,设置了文件夹“webapps”在tomcat中作为MyEclipse的工作区。因此,当我部署一个项目时,它总是出错。而且我无法访问servlet。再次感谢您的帮助!
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}