Servlets servlet 3.0@WebServlet use..web.xml中将包含什么内容?
我想知道在Tomcat7中使用Servlet3.0的目录结构。 我使用了annotation@WebServlet,没有初始化参数 我想知道在web.xml文件中要写什么?? 现在和将来都要写Servlets servlet 3.0@WebServlet use..web.xml中将包含什么内容?,servlets,servlet-3.0,Servlets,Servlet 3.0,我想知道在Tomcat7中使用Servlet3.0的目录结构。 我使用了annotation@WebServlet,没有初始化参数 我想知道在web.xml文件中要写什么?? 现在和将来都要写 该文件存储在tomcat的classes文件夹中。这就是web.xml中所需的全部内容: <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
该文件存储在tomcat的classes文件夹中。这就是
web.xml中所需的全部内容:
<web-app 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"
version="3.0">
兼容3.0的servlet容器(如Tomcat 7)将自动找到@WebServlet
。我从Tomasz Nurkiewicz那里读到了答案,到目前为止,22人投票支持这个答案。请注意,他在4年前回答了
我想知道为什么我需要一个几乎为空的xml文件
我用Servlet 3尝试了hello world
package com.servlet3;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/helloServlet3")
public class HelloServlet3 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("<html><body>");
out.print("<h3>Hello Servlet</h3>");
out.print("</body></html>");
}
}
package com.servlet3;
导入java.io.IOException;
导入java.io.PrintWriter;
导入javax.servlet.ServletException;
导入javax.servlet.annotation.WebServlet;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
@WebServlet(“/helloServlet3”)
公共类HelloServlet3扩展了HttpServlet{
私有静态最终长serialVersionUID=1L;
受保护的void doGet(HttpServletRequest请求、HttpServletResponse响应)
抛出ServletException、IOException{
response.setContentType(“text/html”);
PrintWriter out=response.getWriter();
输出。打印(“”);
打印(“Hello Servlet”);
输出。打印(“”);
}
}
我能够成功地运行这个小小的web应用程序
重要提示:
请注意,此示例中不存在web.xml
因此,我们不需要这种几乎为空的web.xml
但是,如果您需要基于表单的身份验证(但是,没有Spring安全性),则web.xml是必需的
根据这篇文章
,
48.3.1以编程方式验证用户
HttpServletRequest接口的以下方法使您能够
以编程方式对web应用程序的用户进行身份验证
验证允许应用程序对
容器从无约束请求中请求调用方
上下文登录对话框显示并收集用户名和密码
用于身份验证的密码
登录允许应用程序收集用户名和密码
作为指定基于表单的身份验证的替代方案的信息
在应用程序部署描述符中
注销允许应用程序重置呼叫方的呼叫方身份
请求
对于这个annotations@WebServlet??import javax.servlet.annotation.WebServlet,有任何属性是必需的吗;javax包中的servlet、annotation和WebServlet类或接口是什么?@user460920:如果我有一个gradle项目没有正确生成web.xml,那么它们都不是必需的(否则代码甚至不会编译)。。我该怎么补充呢?在STS中的任何项目中都没有看到它。您根本不需要任何web.xml。请看[这里][1]。解释。[1] :问题是…web.xml中将包含什么内容?我的回答是“什么都没有”,对另一个答案的评论有什么不对吗?我很困惑。
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login</form-login-page>
<form-error-page>/login?event=Retry</form-error-page>
</form-login-config>
</login-config>