这篇文章给大家介绍使用IDEA怎么模拟一个Servlet 网络请求,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
首先下载 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 中进行配置:
<web-app ...>
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>demo.Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
</web-app>
其中 servlet-mapping 标签设置对外访问的路径。
然后在 web 目录下添加前端页面文件,比如命名 1.html 作为起始页面,2.html 作为跳转的结果页面。
页面
在 1.html 中编辑页面布局,设置 head 标签,在 body 标签中添加 form表单。
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" >
<title>MyFirst</title>
<script type="text/javascript">
</script>
</head>
<body>
<h2>登录页面(get)</h2>
<form action="/demo" method="get">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="text" name="password" type="hidden">
</td>
</tr>
<tr>
<td colspan="2" >
<input type="submit" value="登录">
</td>
</tr>
</table>
</form>
<h2>登录页面(post)</h2>
<form action="/demo" method="post">
<table>
<tr>
<td>
用户名:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input type="text" name="password" type="hidden">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="登录">
</td>
</tr>
</table>
</form>
</body>
</html>
2.html中编辑页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2 >
登录成功!!!
</h2>
</body>
</html>
最后点击 Debug 进行运行,部署到自己的 Tomcat 服务器上:
Debug
最后在浏览器输入网址: http://localhost:8080/1.html ,就能访问我们部署的网站了。
网站
打开 Chrome 的开发者工具,能够看到发送请求的详细情况:
关于使用IDEA怎么模拟一个Servlet 网络请求就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。