标签:type cte his this 哈哈 targe title 父类 rect
前言:唉,打脸了,前脚刚说过要跟Servlet正式告别,后脚这不又来了,哈哈,总结出一点东西,纠结了一下还是做个分享吧,学习知识比面子重要,对吧。
需求分析
看下面代码,现在有一个UserServlet,里面有增删改查四个方法,按平常的思维,用一个Servlet实现一个功能,这没毛病,现在问题是一个Servlet中有四个功能,那这该如何去实现呢?
package
com.my.servlet;
import
com.my.service.
UserService
;
import
com.my.service.impl.
UserServiceImpl
;
import
javax.servlet.annotation.
WebServlet
;
import
javax.servlet.http.
HttpServlet
;
import
javax.servlet.http.
HttpServletRequest
;
import
javax.servlet.http.
HttpServletResponse
;
@WebServlet
(
"/user"
)
public
class
UserServlet
extends
HttpServlet
{
private
UserService
userService =
new
UserServiceImpl
();
public
String
add(
HttpServletRequest
request,
HttpServletResponse
response){
userService.add();
request.setAttribute(
"mess"
,
"添加成功"
);
return
"r:add.jsp"
;
}
public
String
delete
(
HttpServletRequest
request,
HttpServletResponse
response){
userService.
delete
();
request.setAttribute(
"mess"
,
"删除成功"
);
return
"f:add.jsp"
;
}
public
String
update(
HttpServletRequest
request,
HttpServletResponse
response){
userService.update();
request.setAttribute(
"mess"
,
"修改成功"
);
return
"add.jsp"
;
}
public
String
query(
HttpServletRequest
request,
HttpServletResponse
response){
userService.query();
request.setAttribute(
"mess"
,
"查询成功"
);
return
"add.jsp"
;
}
}
解决思维
解决这个问题其实也简单,再写一个FatherServlet继承于HttpServlet,在这个父类Servlet中的service()方法中通过请求参数判断要调用的子类UserServlet中的哪个方法并执行,可能说到这有些朋友不懂了,如何用参数去判断大家一看下面的代码就明白了,我主要解释一下大家的疑惑,子类UserServlet如何去调用父类FatherServlet中的service()方法,其实秘诀就在这个参数上,在访问UserSerlvet时带上请求参数,访问过来后UserServlet中没有解决参数的方法,所以他会在父类FatherServlet中寻找响应的方法,找到后执行,这是继承的,子类继承父类的方法没毛病吧,所以就解决了。
解决完这个问题后我还在service()方法中加了判断是重定向还是转发的思维,这个就友情赠送了,不说了,来,看代码:
FatherServlet:
package
com.my.servlet;
import
javax.servlet.
ServletException
;
import
javax.servlet.http.
HttpServlet
;
import
javax.servlet.http.
HttpServletRequest
;
import
javax.servlet.http.
HttpServletResponse
;
import
java.io.
IOException
;
import
java.lang.reflect.
InvocationTargetException
;
import
java.lang.reflect.
Method
;
public
class
FatherServlet
extends
HttpServlet
{
@Override
protected
void
service(
HttpServletRequest
req,
HttpServletResponse
resp)
throws
ServletException
,
IOException
{
String
m = req.getParameter(
"m"
);
if
(m==
null
||
""
.equals(m)){
throw
new
RuntimeException
(
"参数不能为空"
);
}
Class
c =
this
.getClass();
try
{
Method
method = c.getMethod(m,
HttpServletRequest
.
class
,
HttpServletResponse
.
class
);
Object
o =method.invoke(
this
,req,resp);
String
rel =
String
.valueOf(o);
//判断返回值的前缀,默认转发,r:重定向 f:转发
if
(rel.startsWith(
"r:"
)){
rel = rel.substring(rel.indexOf(
":"
)+
1
);
resp.sendRedirect(rel);
}
else
if
(rel.startsWith(
"f:"
)){
rel = rel.substring(rel.indexOf(
":"
)+
1
);
req.getRequestDispatcher(rel).forward(req,resp);
}
else
{
req.getRequestDispatcher(rel).forward(req,resp);
}
}
catch
(
NoSuchMethodException
e) {
throw
new
RuntimeException
(
"请求的方法不存在!!"
);
}
catch
(
IllegalAccessException
e) {
throw
new
RuntimeException
(
"请求方法执行异常!!"
);
}
catch
(
InvocationTargetException
e) {
throw
new
RuntimeException
(
"请求方法执行异常!!"
);
}
}
}
UserServlet:
package
com.my.servlet;
import
com.my.service.
UserService
;
import
com.my.service.impl.
UserServiceImpl
;
import
javax.servlet.annotation.
WebServlet
;
import
javax.servlet.http.
HttpServletRequest
;
import
javax.servlet.http.
HttpServletResponse
;
@WebServlet
(
"/user"
)
public
class
UserServlet
extends
FatherServlet
{
private
UserService
userService =
new
UserServiceImpl
();
public
String
add(
HttpServletRequest
request,
HttpServletResponse
response){
userService.add();
request.setAttribute(
"mess"
,
"添加成功"
);
return
"r:add.jsp"
;
}
public
String
delete
(
HttpServletRequest
request,
HttpServletResponse
response){
userService.
delete
();
request.setAttribute(
"mess"
,
"删除成功"
);
return
"f:add.jsp"
;
}
public
String
update(
HttpServletRequest
request,
HttpServletResponse
response){
userService.update();
request.setAttribute(
"mess"
,
"修改成功"
);
return
"add.jsp"
;
}
public
String
query(
HttpServletRequest
request,
HttpServletResponse
response){
userService.query();
request.setAttribute(
"mess"
,
"查询成功"
);
return
"add.jsp"
;
}
}
完整演示
此篇主要是解决题目所示的需求,先在需求解决了,做戏还要做全呢,我就把我测试的代码都整理出来,方便新手去学习。
UserDao
package
com
.
my
.
dao
;
public
interface
UserDao
{
public
void
add
();
public
void
delete
();
public
void
update
();
public
void
query
();
}
UserDaoImpl
package
com
.
my
.
dao
.
impl
;
import
com
.
my
.
dao
.
UserDao
;
public
class
UserDaoImpl
implements
UserDao
{
@Override
public
void
add
()
{
System
.
out
.
println
(
"UserDao中的添加功能实现了"
);
}
@Override
public
void
delete
()
{
System
.
out
.
println
(
"UserDao中的删除功能实现了"
);
}
@Override
public
void
update
()
{
System
.
out
.
println
(
"UserDao中的修改功能实现了"
);
}
@Override
public
void
query
()
{
System
.
out
.
println
(
"UserDao中的查询功能实现了"
);
}
}
UserService
package
com
.
my
.
service
;
public
interface
UserService
{
public
void
add
();
public
void
delete
();
public
void
update
();
public
void
query
();
}
UserServiceImpl
package
com
.
my
.
service
.
impl
;
import
com
.
my
.
dao
.
UserDao
;
import
com
.
my
.
dao
.
impl
.
UserDaoImpl
;
import
com
.
my
.
service
.
UserService
;
public
class
UserServiceImpl
implements
UserService
{
private
UserDao
userDao
=
new
UserDaoImpl
();
@Override
public
void
add
()
{
userDao
.
add
();
}
@Override
public
void
delete
()
{
userDao
.
delete
();
}
@Override
public
void
update
()
{
userDao
.
update
();
}
@Override
public
void
query
()
{
userDao
.
query
();
}
}
UserServlet
package
com
.
my
.
servlet
;
import
com
.
my
.
service
.
UserService
;
import
com
.
my
.
service
.
impl
.
UserServiceImpl
;
import
javax
.
servlet
.
annotation
.
WebServlet
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
javax
.
servlet
.
http
.
HttpServletResponse
;
@WebServlet
(
"/user"
)
public
class
UserServlet
extends
FatherServlet
{
private
UserService
userService
=
new
UserServiceImpl
();
public
String
add
(
HttpServletRequest
request
,
HttpServletResponse
response
){
userService
.
add
();
request
.
setAttribute
(
"mess"
,
"添加成功"
);
return
"r:add.jsp"
;
}
public
String
delete
(
HttpServletRequest
request
,
HttpServletResponse
response
){
userService
.
delete
();
request
.
setAttribute
(
"mess"
,
"删除成功"
);
return
"f:add.jsp"
;
}
public
String
update
(
HttpServletRequest
request
,
HttpServletResponse
response
){
userService
.
update
();
request
.
setAttribute
(
"mess"
,
"修改成功"
);
return
"add.jsp"
;
}
public
String
query
(
HttpServletRequest
request
,
HttpServletResponse
response
){
userService
.
query
();
request
.
setAttribute
(
"mess"
,
"查询成功"
);
return
"add.jsp"
;
}
}
FatherServlet
package
com
.
my
.
servlet
;
import
javax
.
servlet
.
ServletException
;
import
javax
.
servlet
.
http
.
HttpServlet
;
import
javax
.
servlet
.
http
.
HttpServletRequest
;
import
javax
.
servlet
.
http
.
HttpServletResponse
;
import
java
.
io
.
IOException
;
import
java
.
lang
.
reflect
.
InvocationTargetException
;
import
java
.
lang
.
reflect
.
Method
;
public
class
FatherServlet
extends
HttpServlet
{
@Override
protected
void
service
(
HttpServletRequest
req
,
HttpServletResponse
resp
)
throws
ServletException
,
IOException
{
String
m
=
req
.
getParameter
(
"m"
);
if
(
m
==
null
||
""
.
equals
(
m
)){
throw
new
RuntimeException
(
"参数不能为空"
);
}
Class
c
=
this
.
getClass
();
try
{
Method
method
=
c
.
getMethod
(
m
,
HttpServletRequest
.
class
,
HttpServletResponse
.
class
);
Object
o
=
method
.
invoke
(
this
,
req
,
resp
);
String
rel
=
String
.
valueOf
(
o
);
//判断返回值的前缀,默认转发,r:重定向 f:转发
if
(
rel
.
startsWith
(
"r:"
)){
rel
=
rel
.
substring
(
rel
.
indexOf
(
":"
)+
1
);
resp
.
sendRedirect
(
rel
);
}
else
if
(
rel
.
startsWith
(
"f:"
)){
rel
=
rel
.
substring
(
rel
.
indexOf
(
":"
)+
1
);
req
.
getRequestDispatcher
(
rel
).
forward
(
req
,
resp
);
}
else
{
req
.
getRequestDispatcher
(
rel
).
forward
(
req
,
resp
);
}
}
catch
(
NoSuchMethodException
e
)
{
throw
new
RuntimeException
(
"请求的方法不存在!!"
);
}
catch
(
IllegalAccessException
e
)
{
throw
new
RuntimeException
(
"请求方法执行异常!!"
);
}
catch
(
InvocationTargetException
e
)
{
throw
new
RuntimeException
(
"请求方法执行异常!!"
);
}
}
}
add.jsp
<%@
page contentType
=
"text/html;charset=UTF-8"
language
=
"java"
isELIgnored
=
"false"
%>
<%@
taglib prefix
=
"c"
uri
=
"http://java.sun.com/jsp/jstl/core"
%>
<%@
taglib prefix
=
"z"
uri
=
"http://mycompany.com"
%>
<html>
<head>
<title>
测试页面
</title>
</head>
<body>
<h1>
测试
</h1>
${mess}
</body>
</html>
测试展示
越努力越幸运
最后来点鸡汤吧,好久没说了,吃苦的年龄就不要选择安逸,别白了少年头,空悲切,人生赢在转折处,改变从现在开始,分享的越多,你的价值增值越大。
标签:type cte his this 哈哈 targe title 父类 rect
原文地址:https://blog.51cto.com/14886659/2519284