java servlet jsp 实例_Servlet+JSP例子
前面两节已经学习了什么是Servlet,Servlet接口函数是哪些、怎么运行、Servlet生命周期是什么? 以及Servlet中的模式匹配URL,web.xml配置和HttpServlet。怎么在Eclipse中新建一个Servlet工程项目。 今天这里主要是创建一个Servlet+JSP的例子。
一、学习之前补充一下web.xml中配置问题
web.xml中配置((web欢迎页、首页))
用于当用户在url中输入工程名称或者输入web容器url(如http://localhost:8080/)时直接跳转的页面.
welcome-file-list的工作原理是,按照welcome-file的.list一个一个去检查是否web目录下面存在这个文件,如果存在,继续下面的工作(或者跳转到index.html页面,或者配置有struts的,会直接struts的过滤工作).如上例,先去webcontent(这里是Eclipse的工程目录根目录)下是否真的存在index.html这个文件,如果不存在去找是否存在index.jsp这个文件,以此类推。
还要说的是welcome-file不一定是html或者jsp等文件,也可以是直接访问一个action。就像我上面配置的一样,但要注意的是,一定要在webcontent下面建立一个index.action的空文件,然后使用struts配置去跳转,不然web找不到index.action这个文件,会报404错误,
如果配置了servlet的url-pattern是/*,那么访问localhost:8080/会匹配到该servlet上,而不是匹配welcome-file-list;如果url-pattern是/(该servlet即为默认servlet),如果其他匹配模式都没有匹配到,则会匹配welcome-file-list。
例如:
FirstServlet.java
1 packageservlet;2
3 importjava.io.IOException;4 importjava.io.PrintWriter;5
6 importjavax.servlet.ServletException;7 importjavax.servlet.annotation.WebServlet;8 importjavax.servlet.http.HttpServlet;9 importjavax.servlet.http.HttpServletRequest;10 importjavax.servlet.http.HttpServletResponse;11 //@WebServlet("/Firstservlet")
12 public class FirstServlet extendsHttpServlet {13
14 /*(non-Javadoc)15 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)16 */
17 @Override18 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {19 System.out.println("处理get()的请求。。。");20 PrintWriter pw =resp.getWriter();21 pw.write("hello!");22 }23
24 /*(non-Javadoc)25 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)26 */
27 @Override28 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {29
30 }31 }
web.xml 配置
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 ServletTest
4
5 index.html
6 index.htm
7 index.jsp
8 default.html
9 default.htm
10 default.jsp
11
12 ServletTest
13 servlet.FirstServlet
14
15
16 ServletTest
17 /*
18
19
index.jsp
1
2 pageEncoding="ISO-8859-1"%>
3
4
5
6
7
Insert title here
8
9
10 get this first servlet
11
12
如果在上面web.xml里面配置/*, 在浏览器输入:直接匹配到Servlet
如果在上面web.xml里面配置/, 在浏览器输入:可以看出匹配到index.jsp
正常在web.xml里面配置/FirstServlet,会先匹配到index.jsp
二、Servlet+JSP
直接加例子:
1 packagecom.ht.servlet;2
3 public classAccountBean {4 privateString username;5 privateString password;6 /**
7 *@returnthe username8 */
9 publicString getUsername() {10 returnusername;11 }12 /**
13 *@paramusername the username to set14 */
15 public voidsetUsername(String username) {16 this.username =username;17 }18 /**
19 *@returnthe password20 */
21 publicString getPassword() {22 returnpassword;23 }24 /**
25 *@parampassword the password to set26 */
27 public voidsetPassword(String password) {28 this.password =password;29 }30 }31
32 packagecom.ht.servlet;33
34 importjava.io.IOException;35
36 importjavax.servlet.ServletException;37 importjavax.servlet.annotation.WebServlet;38 importjavax.servlet.http.HttpServlet;39 importjavax.servlet.http.HttpServletRequest;40 importjavax.servlet.http.HttpServletResponse;41 importjavax.servlet.http.HttpSession;42
43 /**
44 * Servlet implementation class AccountBean45 */
46 @WebServlet("/CheckAccount")47 public class CheckAccount extendsHttpServlet {48 private static final long serialVersionUID = 1L;49
50 /**
51 *@seeHttpServlet#HttpServlet()52 */
53 publicCheckAccount() {54 super();55 //TODO Auto-generated constructor stub
56 }57
58 /**
59 *@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)60 */
61 protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {62 HttpSession sessionzxl =request.getSession();63 AccountBean account = newAccountBean();64 String username = request.getParameter("username");65 String pwd = request.getParameter("pwd");66 account.setPassword(pwd);67 account.setUsername(username);68 System.out.println("username :"+ username + " password :" +pwd);69 if((username != null)&&(username.trim().equals("jspp"))) {70 System.out.println("username is right!");71 if((pwd != null)&&(pwd.trim().equals("1"))) {72 System.out.println("success");73 sessionzxl.setAttribute("account", account);74 String login_suc = "success.jsp";75 response.sendRedirect(login_suc);76 return;77 }78 }79 System.out.println("fail!");80 String login_fail = "fail.jsp";81 response.sendRedirect(login_fail);82 return;83 }84
85 /**
86 *@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)87 */
88 protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {89
90 doGet(request, response);91 }92
93 }
登录的jsp页面如下Login.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5 String path =request.getContextPath();6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";7 %>
8
9
10
11
12
13
My JSP 'login.jsp' starting page
14
15
16 This is my JSP page.
17
18 username:
19 password:
20
21
22
23
登录成功界面如下success.jsp:
1
2 pageEncoding="UTF-8"%>
3
4
5
6
7 String path =request.getContextPath();8 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";9 %>
10
11
12
13
14
15
My JSP 'success.jsp' starting page
16
17
18
19 username:
20 password:
21 basePath:
22 path:
23
24
登录失败的jsp页面如下:fail.jsp
1
2 pageEncoding="UTF-8"%>
3
4
5 String path =request.getContextPath();6 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";7 %>
8
9
10
11
12
13
My JSP 'fail.jsp' starting page
14
15
16 Login Failed!
17 basePath:
18 path:
19
20
web.xml配置如下:
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 ServletTest
4
5 Login.jsp
6
7
8
9 This is the description of my J2EE component
10 This is the display name of my J2EE component
11 CheckAccount
12 com.ht.servlet.CheckAccount
13
14
15 CheckAccount
16 /login
17
18
描述一下上面运行过程:
在浏览器输入:http://localhost:8080/ServletTest/ 会通过欢迎页面welcome-file-list找到登录页面Login.jsp, 界面显示如下:
在登录页面输入用户名和密码,点击登录,找到对应的action, 会去运行/login其对应的servlet, 找到doGet()方法,判断用户名和密码
如果用户名密码不是jspp和1,就会跳转到失败页面fail.jsp
如果用户名等于jspp和1,则跳转到成功页面success.jsp
Session
上面就是一个最简单的JSP和servlet例子。在运行上面例子中,有一个概念session.
在checkAccount.java中,直接通过request获取session
HttpSession sessionzxl = request.getSession();
后面将定义的变量存储到session中:sessionzxl.setAttribute("account", account);
在jsp中怎么获取session?
在success.jsp中,有这么一行,那么session来至于哪儿?
查看资料后得知,session是jsp隐式对象。
JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明。JSP隐式对象也被称为预定义变量。
JSP所支持的九大隐式对象:
对象描述
request
HttpServletRequest 接口的实例
response
HttpServletResponse 接口的实例
out
JspWriter类的实例,用于把结果输出至网页上
session
HttpSession类的实例
application
ServletContext类的实例,与应用上下文有关
config
ServletConfig类的实例
pageContext
PageContext类的实例,提供对JSP页面所有对象以及命名空间的访问
page
类似于Java类中的this关键字
Exception
Exception类的对象,代表发生错误的JSP页面中对应的异常对象
session是jsp的内置对象,所以你可以直接写在jsp的
String M = session.getAttribute(“a”).toString(); //从session里把a拿出来,并赋值给M
%>
下节添加一个Servlet+jsp+SQL例子。
https://blog.csdn.net/superit401/article/details/51974409
java servlet jsp 实例_Servlet+JSP例子相关推荐
- Java+Servlet+filter+javascript+html+jsp登入注册更新个人信息
eclipse+Java+Servlet+filter+javascript+html+jsp+EL登入注册更新个人信息 数据库sql用vs2008或者mysql用mysql 代码粗糙,提示信息未处理 ...
- java servlet html文件_Servlet生成html页面
Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据,生成动态Web内容. 该Se ...
- java servlet接口开发_servlet三种实现方式之一实现servlet接口
servlet有三种实现方式: 1.实现servlet接口 2.继承GenericServlet 3.通过继承HttpServlet开发servlet 第一种示例代码如下(已去掉包名): import ...
- java servlet 跳转_Servlet跳转方式sendReDirect()和forward()
在web应用服务中,经常会面对不同SERVLET之间的跳转,目前我们可以通过以下两种方式实现: 1.RequestDispatcher.forward() 2.ServletResponse.send ...
- java过滤器应用实例_Servlet过滤器Filter的简单介绍(附示例)
本篇文章给大家带来的内容是关于Servlet过滤器Filter的简单介绍(附示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 特点 1)Filter是依赖于Servlet容器,属 ...
- java 类继承实例_java继承例子代码,java类的继承示例
下面要给大家带来的是一个java类的继承的例子,一起来看看详细的代码吧! 一.题目 1.定义一个Person类,这个类的属性有三个,分别是name.age.color. 类有构造方法给三个属性赋值. ...
- java请求转发实例_Servlet请求转发的步骤和实例
请求转发 一次请求Servlet对应的request对象,在此request中存储数据,可以在此request对象中取出:但是在另一次请求Servlet对应的request对象,是没有第一次请求时在r ...
- java servlet init方法_Servlet详解之两个init方法的作用
servlet如果要求init中什么都不做,重写init时,需要调用super的init吗? 答案是不用,直接init里面什么都不写即可. 适用场景:当前servlet继承于A,A继承于httpser ...
- Java servlet一个最简单的例子
Created by Wang, Jerry, last modified on Dec 10, 2014
最新文章
- Webpack飞行手册
- ES6 Generator async
- VB的一些项目中常用的通用方法-一般用于验证类
- WPF-学习笔记 获取我的文档路径
- 405: HTTP method GET is not supported by this URL
- 写写最近吧,关于读研、找工作
- Codeforces 815C. Karen and Supermarket【树形DP】
- python二级考试选择题公共基础知识_计算机二级Python易忘考点整理
- ntext字段的REPLACE处理示例.sql
- 《算法问题实战策略》-chaper21-树的实现和遍历
- Java常见面试题:对象的访问定位的两种方式
- 360的编码html怎么写,html5之meta charset网页字符编码简写
- Java8 中的真的 Optional 很强大,你用对了吗?
- 五款在线思维导图工具的比较
- 新冠疫情加速医疗废物处置行业发展,我国医废处置能力达6245吨/天
- 迅捷路由器重新设置后服务器无响应,迅捷路由器恢复出厂设置后怎么重新设置...
- eBPF系列学习(4)了解libbpf、CO-RE (Compile Once – Run Everywhe) | 使用go开发ebpf程序(云原生利器cilium ebpf )
- 专访丨兼容国内外市场的代码分析软件,鉴释科技帮助企业减少bug发生率
- mysql查最高薪水,使用子查询查找MySQL Employee表的最高和第二最高薪水?
- 将私人对话发布到公共空间来寻求正义是否可取
热门文章
- eclipse 如何关联git_git的相关操作
- 删除docker私服镜像脚本
- 【Rollo的Python之路】Python 多进程 学习笔记 multiprocessing
- Spark SQL join的三种实现方式
- kmp 模式匹配算法学习笔记
- Ineedle驱动方式dpdk测试性能
- ssh: Could not resolve hostname gitcafe.com: nodename nor servname provided, or not known
- [jquery] 删除文章的时候弹出确认窗口
- 我为什么不无偿加班,你也不应该
- _Default同时存在于两个dll文件中的解决办法