解决Javaweb 提交表单到servlet时出现空白页面,但网站不报错问题
作者:Mayer_从0到1 时间:2021-06-21 20:26:48
问题描述
提交表单到servlet时出现空白页面,但又网站不报错
截图如下
遇到这个问题查找了很多,仍没有解决,刚开始接触IDEA,以为是路径配置问题,去看@WebServlet和XML,捣了半天,发现没问题,就只有这一个servlet访问不了,其他都能行,最后发现是自己doGet方法没加 this.doPost(request, response);
(之前使用eclipse是这段代码自动生成的,用idea没有自动生成就没太注意)
如下图:
package cn.itcast.web.servlet;
import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.service.impl.UserServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取id
String id = request.getParameter("id");
// 2.通过id查找User
UserService service = new UserServiceImpl();
User user = service.findUserById(id);
// 3.存储
request.setAttribute("user",user);
// 4.转发
request.getRequestDispatcher("/update.jsp").forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
解决方法
doGet方法里加上
this.doPost(request, response);
立马见效
下次再遇到此类问题,自己要多去debug下,别人出现的问题可能一样,但是造成问题的原因很可能不相同。
如果有其他原因的可以讨论下。
javaweb后端调用servlet出现空白
例子:
Login.html跳转LoginServlet
Tomcat生成该项目的名称是web_war ,则页面跳转的时候 都要加上/web_war作为路径之一
例如
login.html跳转时 action="/web_war/LoginServlet"
所以最终url访问路径一定是/项目名_war/访问的资源
来源:https://blog.csdn.net/weixin_42174815/article/details/90937689
标签:Javaweb,提交表单,servlet,空白页
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
利用java实现二叉搜索树
2023-10-25 13:25:07
![](https://img.aspxhome.com/file/2023/0/61020_0s.png)
java 多态性详解及常见面试题
2023-03-15 18:08:49
Spring中多配置文件及引用其他bean的方式
2023-07-01 17:31:03
![](https://img.aspxhome.com/file/2023/5/62715_0s.jpg)
SpringBoot Security密码加盐实例
2023-06-08 17:06:48
简单谈谈java自定义注解
2021-07-21 18:40:53
mybatis教程之resultmap_动力节点Java学院整理
2022-09-05 22:38:37
Java 实战项目之在线点餐系统的实现流程
2022-08-05 04:11:35
![](https://img.aspxhome.com/file/2023/2/64802_0s.png)
深入理解Java并发编程之ThreadLocal
2023-11-21 02:43:42
![](https://img.aspxhome.com/file/2023/8/59018_0s.jpg)
C#四种计时器Timer的区别和用法
2021-10-12 01:31:14
浅谈Servlet开发技术基础
2023-01-17 04:03:15
Spring-Security实现登录接口流程
2023-03-24 05:35:40
![](https://img.aspxhome.com/file/2023/4/70334_0s.png)
C# 字符串与unicode互相转换实战案例
2023-12-23 09:29:54
Android基础之使用Fragment控制切换多个页面
2023-07-11 00:08:37
SpringMVC接收多个对象的4种方法
2023-11-23 06:24:18
Java并发编程之闭锁与栅栏的实现
2022-01-09 15:00:12
Spring Data环境搭建实现过程解析
2022-02-26 20:13:38
WebService教程详解(二)
2022-02-24 02:26:54
Java Socket编程详解及示例代码
2022-06-29 07:05:27
Java与C++分别用递归实现汉诺塔详解
2021-10-23 01:28:59
![](https://img.aspxhome.com/file/2023/7/62237_0s.png)
Java如何实现Word文档分栏效果
2023-09-11 00:28:53
![](https://img.aspxhome.com/file/2023/8/83008_0s.png)