Servlet(一):Hello Servlet
今天介绍的专题是servlet和jsp,属于web开发中的基础,先来实际操作一下servlet,创建你的第一个web小项目。
1、新建一个maven项目。
鼠标右键单击eclipse左边项目栏的空白区,选择第一个new,在出现的下拉中选择other,在新弹出的框中选择maven,点开maven选择maven project,点击next。在新弹出的框中勾选上面的第一个按钮Create a simple project (skip archetype selection),再点击next。在新弹出的框中,第一行Group Id,这里可以是项目的部分地址或者公司的部分网址,如com.baidu等,只是要反写;第二行Artifact Id,这是项目名,比如我们现在要建的项目,可以叫servlet_day01;第三行的Version是版本;第四行Packaging是项目打包的方式,选择war包即可。剩下的输入栏可以先不用管。
建完项目后,会发现项目带了个红叉,解决办法是,点击项目名称,鼠标右键,找到许多选项中的Java EE Tools,在新出现的下拉选中选择Generate Deployment Descriptor Stub,红叉就会消失。
2、对项目进行相关配置
首先配置jre,项目名称右键,选中Build Path,在出现的下拉选中选择Configure Build Path,首先移除旧的jre系统库(新建maven项目默认是1.5的系统库),点击右边的Add Library,在新出现的框中选择JRE System Library,在新出现的框中找到自己对应的jre安装路径,点击finish即可。
再来配置Tomcat,还是在Java Build Path的页面,同样点击Add Library,在新出现的框中选择Server Runtime,选择你安装好的Tomcat(文末会有一篇关于新建Maven项目的教程,可以照着步骤操作),点击finish。
3、新建一个servlet类
在src/main/java这个文件夹下,新建一个包,我们取名servlet,在servlet包下新建一个class,名叫HelloServlet,在它的父类那里,点击右边的Browse,在新弹出的框中输入HttpServlet,选中HttpServlet,点击ok,然后点击finish。
重写doGet()方法和doPost()方法。在HelloServlet类里面,鼠标右键单击(或者快捷键alt+shift+s),找到Source选项,在新出现的下拉选中选择Override/Implement Methods,在新出现的弹框中勾上doPost方法和doGet方法,点击ok即可。
4、编写jsp页面
jsp页面存放的位置在src-->main-->webapp路径下,右键点击webapp文件夹,选择new-->other,在弹出来的框中,点开Web,选择JSP File选项,点击next,输入文件名,如hello.jsp,点击finish即可。
将页面中存在ISO-8859-1的地方替换为UTF-8,更改编码格式。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一个servlet例子</title>
</head>
<body style="margin:50px;"> <!-- 演示get请求 -->
<a href="helloServlet">Get请求</a> <!-- 演示post请求 -->
<form action="helloServlet" method="post">
<label>姓名</label>
<input type="text" name="name"/>
<br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
5、重写doGet()方法和doPost()方法
/**
* 第一个servlet例子
* @author 小川94
* @date 2018年6月2日
*/
public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 打印日志
System.out.println("处理Get()请求...");
// 设置服务器返回的数据类型
response.setContentType("text/html");
// 获取一个输出流
PrintWriter out = response.getWriter();
// 输出数据,将数据发送到浏览器
out.println("<h1>Hello Servlet!</h1>");
// 关闭流
out.close();
} @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 打印日志
System.out.println("处理Post()请求...");
// 对从浏览器获取的数据设置编码格式
request.setCharacterEncoding("UTF-8");
// 获取页面传入的数据
String name = request.getParameter("name");
System.out.println(name);
// 设置服务器返回的数据类型
response.setContentType("text/html;charset=UTF-8");
// 获取一个输出流
PrintWriter out = response.getWriter();
// 输出数据,将数据发送到浏览器
out.println("<h1>Hello "+name+"!</h1>");
// 关闭流
out.close();
} }
6、配置web.xml文件
在src-->main-->webapp-->WEB-INF路径下找到web.xml文件,添加<servlet></servlet>、<servlet-mapping></servlet-mapping>标签。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>servlet_day01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <servlet>
<!-- servlet别名,可以随意定义,但是为了规范,命名要有实际意义 -->
<servlet-name>hello</servlet-name>
<!-- servlet类路径,由包名.类名组成 -->
<servlet-class>servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<!-- servlet别名,可以随意定义,但是为了规范,命名要有实际意义 -->
<servlet-name>hello</servlet-name>
<!-- jsp页面请求路径 -->
<url-pattern>/helloServlet</url-pattern>
</servlet-mapping> </web-app>
7、部署测试
项目名右键单击,Run As --> Run on Server,在弹出的框中选择Tomcat,点击finish即可。
项目启动成功后,打开浏览器,在地址栏输入http://localhost:8088/servlet_day01/hello.jsp,就可以看到之前写的jsp页面,可以对刚刚写的doPost()方法和doGet()方法进行测试。地址信息不要输错,不然会报404错误。
文章首发于我的个人公众号:悦乐书。喜欢分享一路上听过的歌,看过的电影,读过的书,敲过的代码,深夜的沉思。期待你的关注!
公众号后台输入关键字“Java学习电子书”,即可获得12本Java学习相关的电子书资源,如果经济能力允许,还请支持图书作者的纸质正版书籍,创作不易。
Servlet(一):Hello Servlet的更多相关文章
- javaweb项目jsp跳转servlet Error instantiating servlet class 问题
问题: HTTP Status 500 - Error instantiating servlet class RecommenderServlet type Exception report mes ...
- Servlet过滤器,Servlet过滤器创建和配置
第一:Servlet的过滤器的创建和配置,创建一个过滤器对象需要实现javax.servlet.Filter接口,同时实现Filter的3个方法. 第一方法是过滤器中的init()方法用 ...
- Servlet简介与Servlet和HttpServlet运行的流程
1.Servlet [1] Servlet简介 > Server + let > 意为:运行在服务器端的小程序. > Ser ...
- Java Servlet系列之Servlet生命周期
Servlet生命周期定义了一个Servlet如何被加载.初始化,以及它怎样接收请求.响应请求,提供服务.在讨论Servlet生命周期之前,先让我们来看一下这几个方法: 1. init()方法 在Se ...
- javaWeb中servlet开发(3)——Servlet生命周期
生命周期:是一个程序的存在周期,servlet由于是受容器的管理,所以容器来决定其生命周期 1.servlet生命周期 2.servlet生命周期对应的方法 3.servlet生命周期代码 publi ...
- Java Servlet(四):Servlet接口service工作(ServletRequest,ServletResponse对象)(jdk7+tomcat7+eclipse)
本篇将会记录,Servlet接收客户端传递来的参数信息,并返回信息使用的对象,及这些对象的函数相关用法. 还是在java ee工程中进行操作,在WebContent目录下创建一个login.jsp文件 ...
- 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix
严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...
- javaWeb中servlet开发(2)——servlet与表单
1.重写doGet方法 public class InputServlet extends HttpServlet{ public void doGet(HttpServletRequest req, ...
- 报javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception异常 的解决方案
后台错误信息如下: javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception org.a ...
- Java Servlet(三):Servlet中ServletConfig对象和ServletContext对象
本文将记录ServletConfig/ServletContext中提供了哪些方法,及方法的用法. ServletConfig是一个抽象接口,它是由Servlet容器使用,在一个servlet对象初始 ...
随机推荐
- NSOperation讲解
本篇博客转自https://www.jianshu.com/p/4b1d77054b35 1. NSOperation.NSOperationQueue 简介 NSOperation.NSOperat ...
- 跨域学习笔记3--web.config设置之system.webServer 详细介绍,为网站设置默认文档
自己并不懂,在此先记录下来,留待以后学习... 如何:为 IIS 7.0 配置 <system.webServer> 节2008-06-14 22:26http://technet.mic ...
- [转]Angular4 数据请求 POST、GET
本文转自:https://blog.csdn.net/dailuwen/article/details/79375980 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...
- 如何定义一个有效的OWIN Startup Class
命名约定 Katana在程序集内的程序集名称空间下查找一个叫做Startup的类, 通过属性指定 [assembly: OwinStartup(typeof(OwinConsoleApp.Startu ...
- [PHP] PHP闭包(closures)
1.闭包函数也叫匿名函数,一个没有指定名称的函数,一般会用在回调部分 2.闭包作为回调的基本使用, echo preg_replace_callback('~-([a-z])~', function ...
- 【redis】6、redis常用命令
[开启redis客户端,执行redis命令] redis-cli -h 192.168.1.27 -a HoomSun1 [批量执行redis命令.把命令写到txt中,批量执行] cat /t ...
- 10个JavaScript常见BUG及修复方法
译者按: JavaScript语言设计太灵活,用起来不免要多加小心掉进坑里面. 原文: Top 10 bugs and their bug fixing 译者: Fundebug 为了保证可读性,本文 ...
- elementUI 时间格式化(一般方法)
1.html: ... <el-table-column prop="updateTime" label="更新时间" width="160&q ...
- SPOJ1811 LCS - Longest Common Substring(后缀自动机)
A string is finite sequence of characters over a non-empty finite set Σ. In this problem, Σ is the s ...
- springboot 开发 Tars
1,创建 springboot 项目,并在启动类添加 @EnableTarsServer 注解 @SpringBootApplication @EnableTarsServer public clas ...