您好,欢迎来到爱go旅游网。
搜索
您的当前位置:首页servlet_多方法

servlet_多方法

来源:爱go旅游网
Servlet的多方法实现(反射)

import java.io.IOException;

import java.lang.reflect.Method;

import javax.servlet.ServletException; import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class SuperServlet extends HttpServlet {

@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException { doPost(req, resp); }

@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp)

throws ServletException, IOException {

req.setCharacterEncoding(\"gbk\");

resp.setContentType(\"text/html;charset=gbk\"); //编码只设置一次

String methodName = req.getParameter(\"method\");

if(methodName==null)

methodName = \"viewAll\"; try {

Method method this.getClass().getDeclaredMethod(methodName,

=

HttpServletRequest.class,HttpServletResponse.class);//获得被申明的方法

method.invoke(this, req,resp);//执行方法 }catch(Exception e){ e.printStackTrace(); } } }

import java.io.IOException;

import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class UserServlet extends SuperServlet {

public void viewAll(HttpServletRequest request,HttpServletResponse response) throws IOException{

response.getWriter().println(\"is_viewAll\"); }

public void addUser(HttpServletRequest request,HttpServletResponse response) throws IOException{ response.getWriter().println(\"is_addUser\"); } }

xmlns=\"http://java.sun.com/xml/ns/javaee\"

xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\">

This is the description of my component

This is the display name of my component

UserServlet UserServlet

UserServlet /user.do

index.jsp

J2EE J2EE

http://localhost:8080/ServletS/user.do

http://localhost:8080/ServletS/user.do?method=addUser 源码下载地址

http://www.baidupcs.com/file/677f26c17b84ec53a95b1e0c45587cd1?xcode=fc10d6bfa6a537a9b666b80c53ccaa79befa6f4adf466577&fid=3809226185-250528-792485143&time=1386294329&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-QNYZV4Xm3O1GU3cZQKrIvZA9jRk%3D&to=wb&fm=Q,B,T,t&expires=8h&rt=pr&r=3934663&logid=8239467&vuk=3809226185&fn=ServletS.zip

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igat.cn 版权所有 赣ICP备2024042791号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务