您的位置:首页 > 娱乐 > 八卦 > Java基础入门day54

Java基础入门day54

2025/1/11 9:41:03 来源:https://blog.csdn.net/zpz2001/article/details/139177242  浏览:    关键词:Java基础入门day54

day54

servlet升级03

特点

当前设计又有一个问题,我们目前可以做到一个实体类用一个servlet,比如Student类的所有crud方法都可以在StudentServlet中的service方法中进行动态处理。假如又有User类,我们就要在UserServlet中设计service方法,在该service方法中再次进行动态方法的判断处理

假如项目有几百个实体类,可能对应几百个Servlet,可能就要写几百个service方法的动态处理

通过分析,我们发现,可以将op的值特意设置为各个实体类的方 法名

使用反射动态获取每一个servlet,通过发射得到每一个servlet后,动态获取op的值,再来动态觉得调用各个op值所对应的方法

反射

“照妖镜”,一个类中有四个访问级别的成员,private只有自己能访问,default自己和通过的类可以访问,protected是自己,同包类和非同包子类可以访问,public是当前项目所有类都可以访问

反射可以在类的外部拿到任何访问修饰基本的数据成员

反射功能强大,但是效率低

service方法的重构

使用反射来实现service方法的简化

package com.saas.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 BaseServlet extends HttpServlet {
​@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=UTF-8");
​String op = req.getParameter("op");
​System.out.println(op);
​Class<? extends BaseServlet> clazz = getClass();
​try {Method method = clazz.getDeclaredMethod(op, HttpServletRequest.class, HttpServletResponse.class);
​//  设置方法可以被访问method.setAccessible(true);
​//  使用反射调用对应的方法method.invoke(this, req, resp);} catch (NoSuchMethodException e) {throw new RuntimeException(e);} catch (IllegalAccessException e) {throw new RuntimeException(e);} catch (InvocationTargetException e) {throw new RuntimeException(e);}}
}

任何继承自当前BaseServlet的servlet都会执行该BaseServlet的生命周期方法service方法

在service方法中,动态获取用户传递过来的op值

而该op值一定要设置为每个servlet中的方法名

运用反射得到op对应的方法对象,每个方法的参数列表都是HttpServletRequest和HttpServletResponse

得到每一个业务方法对象后,设置方法可以在外界被访问

并调用invoke实现对于每个业务方法的调用

好处是当前项目只需要做一个op的判断,无需再在每个servlet里面有关于op判断的重复代码

package com.saas.servlet;
​
import com.saas.entity.User;
import com.saas.service.IUserService;
import com.saas.service.impl.UserServiceImpl;
​
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
​
@WebServlet("/User")
public class UserServlet extends BaseServlet{
​private IUserService userService = new UserServiceImpl();
​public void login(HttpServletRequest req, HttpServletResponse resp){System.out.println("UserServlet login");}public void register(HttpServletRequest req, HttpServletResponse resp){System.out.println("UserServlet register");}public void logout(HttpServletRequest req, HttpServletResponse resp){System.out.println("UserServlet logout");}
​protected void saveUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{System.out.println("UserServlet saveUser");}
​protected void updateUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{System.out.println("UserServlet updateUser");}
​protected void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{System.out.println("UserServlet deleteUser");}
​protected void getUserByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{System.out.println("UserServlet getUserByUid");
​String suid = req.getParameter("uid");int uid = suid == null ? 0 : Integer.parseInt(suid);
​User u = userService.getUserByUid(uid);
​PrintWriter out = resp.getWriter();
​out.print("<form>");out.print("<input type='hidden' name='uid' value='" + u.getUid() + "' />");out.print("<input type='text' name='username' value='" + u.getUsername() + "' />");out.print("<input type='text' name='password' value='" + u.getPassword() + "' />");out.print("<input type='text' name='email' value='" + u.getEmail() + "' />");out.print("<input type='text' name='phone' value='" + u.getPhone() + "' />");out.print("<input type='text' name='address' value='" + u.getAddress() + "' />");out.print("<input type='submit' value='update' />");out.print("</form>");
​}
​protected void getAllUsers(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{System.out.println("UserServlet getAllUsers");
​List<User> users = userService.getAllUsers();
​PrintWriter out = resp.getWriter();
​out.print("<table border=1 align='center' width='80%'>");out.print("<tr>");out.print("<th>uid</th>");out.print("<th>username</th>");out.print("<th>password</th>");out.print("<th>email</th>");out.print("<th>phone</th>");out.print("<th>address</th>");out.print("<th>manage</th>");out.print("</tr>");for (User user : users) {out.print("<tr>");out.print("<td>" + user.getUid() + "</td>");out.print("<td>" + user.getUsername() + "</td>");out.print("<td>" + user.getPassword() + "</td>");out.print("<td>" + user.getEmail() + "</td>");out.print("<td>" + user.getPhone() + "</td>");out.print("<td>" + user.getAddress() + "</td>");out.print("<td><a href='User?op=getUserByUid&uid=" + user.getUid() + "'>update</a></td>");out.print("</tr>");}out.print("</table>");}
}
package com.saas.servlet;
​
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
​
@WebServlet("/Student")
public class StudentServlet extends BaseServlet {
​private static final long serialVersionUID = 1L;
​public void getAllStudents(HttpServletRequest req, HttpServletResponse resp) {System.out.println("StudentServlet getAllStudents");}
​
​public void getStudentBySid(HttpServletRequest req, HttpServletResponse resp) {System.out.println("StudentServlet getStudentBySid");}
​
​public void getStudentsByPage(HttpServletRequest req, HttpServletResponse resp) {System.out.println("StudentServlet getStudentsByPage");}
​
​public void saveStudent(HttpServletRequest req, HttpServletResponse resp) {System.out.println("StudentServlet saveStudent");}
​
​public void updateStudent(HttpServletRequest req, HttpServletResponse resp) {System.out.println("StudentServlet updateStudent");}
​
​public void deleteStudent(HttpServletRequest req, HttpServletResponse resp) {System.out.println("StudentServlet deleteStudent");}
}

在UserServlet和StudentServlet中都将不会出现op的判断处理,只需要在BaseServlet中实现一次即可,代码大大简化

        

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com