Servlet是在服务器机器上运行的Java应用程序编程接口(API),它可以拦截客户端发出的请求,并可以相应地生成/发送响应。
我有一个Java桌面应用程序(jdk8,但我可以升级没有问题),它循环运行并通过SocketChannel与其他设备通信。还有一个使用
我使用 Eclipse Maven 插件创建一个 Java EE 7 项目。我的问题是,当我运行应用程序时,不会调用实现 SerlvetContextListener 的类。是什么原因导致这个问题...
@WebFilter(urlPatterns) 未正确映射 HTTP 请求
我有一个带有 @GetMapping(value = "/getToken") 的函数,可以写入 JSON 内容。 @GetMapping(值=“/getToken”) 公共字符串 getToken(HttpServletRequest 请求,
从 HttpServletRequest 获取 POST 请求正文
我正在尝试从 HttpServletRequest 对象获取整个主体。 我遵循的代码如下所示: if ( request.getMethod().equals("POST") ) { StringBuffer sb = new StringBuffer()...
在 Eclipse 中,当我尝试通过实现 Servlet 接口来创建 Servlet 时,注释方法在运行时不会创建正确的链接
导入java.io.IOException; 导入javax.servlet.Servlet; 导入 javax.servlet.ServletConfig; 导入 javax.servlet.ServletException; 导入 javax.servlet.ServletRequest; 导入 javax.servlet。
ServletResponse Redirect 在 302 响应中显示应用程序服务器地址
为什么在显示我的应用程序服务器 IP 地址及其端口的重定向后发送“隐藏”302 响应(参见上图) 这被
使用 ContentCachingResponseWrapper 丢失标头
我必须记录响应正文和响应标头。为此,我使用 Interceptor 和 ContentCachingResponseWrapper 类(我也尝试过使用过滤器,但结果相同)。当我将两者结合起来时,我
我正在创建一个用于缓存的 servlet 过滤器。这个想法是将响应主体缓存到memcached。响应正文由以下方式生成(结果是一个字符串): response.getWriter().print(结果); 我的问题...
ContentCachingResponseWrapper 生成空响应
我正在尝试实现过滤器来记录 Spring MVC 应用程序中的请求和响应。 我使用以下代码: @成分 公共类 LoggingFilter 扩展了 OncePerRequestFilter { 优先...
无法初始化类 org.apache.maven.plugin.war.util.WebappStructureSerializer -Maven 配置问题 有解决方案吗?
当我创建 Maven 项目时,我在 pom.xml 中收到此错误 无法初始化类 org.apache.maven.plugin.war.util.WebappStructureSerializer -Maven配置问题 web.xml 是
Spring 过滤器:获取 servlet url-pattern
想象一下我已经定义了这些 REST 端点: @RequestMapping(路径=“/用户”) @RestController 公共类 ConfigUserController { [...] @GetMapping(路径 = "/变量/&q...
从 HttpServletRequest 检索 JSON 对象文字
我正在编写需要提取发布到 servlet 的对象文字的代码。我已经研究了 HttpServletRequest 对象的 API,但我不清楚如何从中获取 JSON 对象......
Ant Javac 和 Commandline Javac 给出不同的结果
我有一个导入一些 servlet 库的类。当我从命令行编译它时,一切都很好。 当我使用ant编译任务来编译它时,它给出了找不到servlet的错误
是否存在 QueryString 存在但 HttpServletRequest.getParameterMap() 为空的情况?
当我们在测试环境中进行压力测试时,我遇到了一个奇怪的情况。当应用程序负载较高时,查询字符串偶尔会丢失,Spring 会抛出
我在将类导入 .jsp 文件时遇到问题。当我尝试导入时,程序继续运行,但无法访问变量。我正在尝试将 GameServlet.java 类导入 gam...
我在将类导入 .jsp 文件时遇到问题。当我尝试导入时,程序继续运行,但无法访问变量。我正在尝试将 GameServlet.java 类导入 gam...
我在将类导入 .jsp 文件时遇到问题。当我尝试导入时,程序继续运行,但无法访问变量。我正在尝试将 GameServlet.java 类导入 gam...
修复 Spring Boot 应用程序中的“找不到接口 javax.servlet.http.HttpServletResponse 的主要或单个唯一构造函数”
我想为我的应用程序构建一个注册和登录模块,但遇到了标题中的错误。 我的登录.html 我想为我的应用程序构建一个注册和登录模块,但遇到了标题中的错误。 我的登录.html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Login</title> </head> <body> <h1>Login</h1> <form th:action="@{/login}" th:object="${login}" method="post"> Username: <input type="text" th:field="*{username}"><br/> <p th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></p> Password: <input type="password" th:field="*{password}"><br/> <p th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></p> <button type="submit">Login</button> </form> </body> </html> 我的用户类别: package com.webprojekt.webblog.DAO; import jakarta.persistence.*; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Size; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.util.List; @Data @Entity(name = "user") @Table @Builder @AllArgsConstructor @NoArgsConstructor public class User { @Id @SequenceGenerator( name = "user_sequence", sequenceName = "user_sequence", allocationSize = 1 ) @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "user_sequence" ) @Column( nullable = false ) private long id; @Column( name = "name", nullable = true, columnDefinition = "TEXT" ) private String name; @Column( name = "is_user_admin" ) boolean isAdmin; @Column( name = "username" ) @NotEmpty @Pattern(regexp = "^[a-zA-Z0-9_]+$", message = "Only letters, digits and underscores allowed") private String username; @Column( name ="password" ) @Size(min = 5, message = "your password must have at least 5 characters") private String password; @Transient private String password2; @OneToMany(mappedBy = "user") private List<Entry> entry; @OneToMany(mappedBy = "user") private List<Comment> comment; public User(String name) { this.name = name; this.isAdmin=false; } //DTO Login public User(String username, String password1) { this.username = username; this.password = password1; } //DTO registration public User( String username, String password1, String password2) { this.username = username; this.password = password1; this.password2 = password2; } public User(String name, String username, String password, String password2) { this.name = name; this.username = username; this.password = password; this.password2 = password2; } } 我的课程: package com.webprojekt.webblog.DAO; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.ManyToOne; import jakarta.persistence.Table; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import java.time.Instant; import java.util.UUID; @Builder @NoArgsConstructor @AllArgsConstructor @Table @Data @Entity public class Session { @Id private String id = UUID.randomUUID().toString(); @ManyToOne private User user; private Instant expiresAt; public Session(User user, Instant expiresAt) { this.user = user; this.expiresAt = expiresAt; } public User getUser() { return user; } public String getId() { return id; } public void setExpiresAt(Instant expiresAt) { this.expiresAt = expiresAt; } } 和会话控制器: package com.webprojekt.webblog.API; import com.webprojekt.webblog.BussinesLayer.WebBlogServices; import com.webprojekt.webblog.DAO.Session; import com.webprojekt.webblog.DAO.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; import java.time.Instant; import java.util.Optional; @Controller public class SessionController { private WebBlogServices webBlogServices; @Autowired public SessionController(WebBlogServices webBlogServices) { this.webBlogServices = webBlogServices; } @GetMapping("/login") public String login(Model model) { model.addAttribute("login", new User ("", "")); return "login"; } @PostMapping("/login") public String login(@ModelAttribute("login") User user, BindingResult bindingResult, HttpServletResponse response) { Optional<User> optionalUser = webBlogServices.findByUsernameAndPassword(user.getUsername(), user.getPassword ()); if (optionalUser.isPresent()) { Session session = new Session(optionalUser.get(), Instant.now().plusSeconds(7*24*60*60)); //expires one week from now webBlogServices.addSession(session); //store the session ID in a cookie to keep the username secret Cookie cookie = new Cookie("sessionId", session.getId()); response.addCookie(cookie); // Login successful return "redirect:/"; } bindingResult.addError(new FieldError ("login", "password", "Login not successful.")); return "login"; } @PostMapping("/logout") public String logout(@CookieValue(value = "sessionId", defaultValue = "") String sessionId, HttpServletResponse response) { Optional<Session> optionalSession = webBlogServices.findByIdAndExpiresAtAfter(sessionId, Instant.now()); optionalSession.ifPresent(session -> webBlogServices.delete(session)); Cookie cookie = new Cookie("sessionId", ""); cookie.setMaxAge(0); response.addCookie(cookie); return "redirect:/"; } } 控制台日志: 2023-02-26T18:44:00.439+01:00 ERROR 10952 --- [nio-8080-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse] with root cause java.lang.IllegalStateException: No primary or single unique constructor found for interface javax.servlet.http.HttpServletResponse at org.springframework.beans.BeanUtils.getResolvableConstructor(BeanUtils.java:266) ~[spring-beans-6.0.4.jar:6.0.4] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:219) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:85) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:147) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:181) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:148) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:884) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:973) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) ~[spring-webmvc-6.0.4.jar:6.0.4] at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:914) ~[spring-webmvc-6.0.4.jar:6.0.4] at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:731) ~[tomcat-embed-core-10.1.5.jar:6.0] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885) ~[spring-webmvc-6.0.4.jar:6.0.4] at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:814) ~[tomcat-embed-core-10.1.5.jar:6.0] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:223) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-10.1.5.jar:10.1.5] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-6.0.4.jar:6.0.4] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116) ~[spring-web-6.0.4.jar:6.0.4] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:185) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:158) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:177) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:119) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:400) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:859) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1734) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-10.1.5.jar:10.1.5] at java.base/java.lang.Thread.run(Thread.java:1589) ~[na:na] 到目前为止,我可能尝试了通过谷歌找到的所有解决方案,但没有得到解决方案。这是我第一次使用 Lombok 和 HttpServletResponse 等技术。我知道我无法在java中实例化接口,但我不知道如何解决它。提前谢谢你们了。 编辑1: 我的 pom.xml: <?xml version="1.0" encoding="UTF-8"?> 4.0.0 org.springframework.boot spring-boot-starter-父级 3.0.2 塔利亚社区 图书馆 0.0.1-快照 图书馆 图书馆 17 org.springframework.boot spring-boot-starter-数据-jpa org.springframework.boot spring-boot-starter-jdbc <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>4.0.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> Spring Boot 3(和 Spring Framework 6)需要 Jakarte EE 10 的基线。您不能将其与低于该版本的 Java EE 或 Jakarte EE 版本一起使用。 您必须从 jakarta.servlet-api 中删除对 pom.xml 的显式依赖。 Java Servlet 4 低于基线,特别是仍然使用以 javax.servlet 开头的包名称。 如果删除显式依赖项,Spring 将传递正确的依赖项。然后,您需要将所有以 javax.servlet 开头的导入替换为 javax 替换为 jakarta,例如 import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletResponse; 将所有以 javax.servlet 开头的导入替换为由 jakarta 替换的 javax, 例如 import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletResponse; 你最好使用 jakarta 包替换所有 javax.servlet 我也这么认为
我正在使用一种方法从数据库获取一些信息并返回包含该数据的对象。 当我在 Servlet doGet 上使用此方法时,对象变为空。当我在...
我有一个业务逻辑层,我想在 servlet 中使用它,但我所做的任何事情都会引发错误: @WebServlet("/Servlet") 公共类 Servlet 扩展 HttpServlet { 私人静态