这篇文章给大家介绍使用IDEA怎么模拟一个Servlet 网络请求,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
我们提供的服务有:成都网站制作、成都做网站、微信公众号开发、网站优化、网站认证、包河ssl等。为上千余家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的包河网站制作公司
首先下载 IntelliJ IDEA 集成工具
接下来配置 Tomcat 服务器,以 Mac 电脑为例,参考:Mac上tomcat服务器安装配置 。
然后打开 IntelliJ IDEA ,选择右边的 Java Enterprise 项目类型,选择刚装的 Tomcat 服务器,勾选 Web Application 选项。

新建工程
点选 next,输入自定义工程名称 demo:

工程
然后我们就能看到新建工程的全貌:

工程
至此,一个 Web 应用工程的框架已经做好。但是要顺利部署到 Tomcat 服务器,还需要我们添加处理服务的对象 Servlet。点击 src 文件夹,添加 Servlet:

servlet
Servlet 类中能看到默认生成的 doGet 和 doPost 方法:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
response.setContentType("text/html");
response.getWriter().print("收到 post 请求");
String username = request.getParameter("username");
String pwd = request.getParameter("password");
if("admin".equals(username) && "abc123".equals(pwd)) {
response.sendRedirect("/2.html");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");//设置
response.setContentType("text/html");
String username = request.getParameter("username");
String pwd = request.getParameter("password");
if("admin".equals(username) && "abc123".equals(pwd)) {
response.sendRedirect("/2.html");
}
}要想使用新建的 Servlet 类,还需要在 web.xml 中进行配置:
Servlet demo.Servlet Servlet /demo
其中 servlet-mapping 标签设置对外访问的路径。
然后在 web 目录下添加前端页面文件,比如命名 1.html 作为起始页面,2.html 作为跳转的结果页面。

页面
在 1.html 中编辑页面布局,设置 head 标签,在 body 标签中添加 form表单。
MyFirst 登录页面(get)
登录页面(post)
2.html中编辑页面:
Title 登录成功!!!
最后点击 Debug 进行运行,部署到自己的 Tomcat 服务器上:

Debug
最后在浏览器输入网址: http://localhost:8080/1.html ,就能访问我们部署的网站了。

网站
打开 Chrome 的开发者工具,能够看到发送请求的详细情况:

关于使用IDEA怎么模拟一个Servlet 网络请求就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
当前标题:使用IDEA怎么模拟一个Servlet网络请求
网站链接:http://www.jxjierui.cn/article/ghpdpj.html


咨询
建站咨询
