java 反射的应用 在servlet上

1.集合类
    List    ArrayList解决了数组的缺陷。数组的大小是固定,数组的类型是固定的
    Set        HashSet    所有的元素是唯一的 a=“a” b="a"   "a"  
    Map        是List和Set的集合,数据非常快
2.多线程
    Thread类
    Runnable接口

    Thread t=new MyThread();
    修改run方法
    使用start()

    new Runable1()
    {
        public void run(){}
    }
3.网络编程
    1.三要素
        ip        主机
        端口        0-65535    0-1023
        协议        tcp/udp
        tcp必须三次握手之后,建立稳固的链接,才回开始信息通讯

    2.信息通讯
        ServerSocket ss=new ServerSocket(7777);
        Socket s=ss.accept();
        i/o
        InputStream is=s.getInputStream()
        OutputStream os=s.getOutputStream()

        byte[]bytes=new byte[1024];
        int len=is.read(bytes);

        os.write("abc".getBytes());


        Socket s=new Socket("127.0.0.1",7777);
4.反射

    1.读取正字啊运行类的方法和属性
        1.Class对象
            Class c=Class.forName("co.goho.yuanyu.pojo.Student");
            Class c0=Student.class;
            Class c1=student.getClass();
        2.
            c.getConstructors();
            c.getMethods();
            c.getFields();
            c.getDeclaredMethods();


        获得名称为a,参数为int的方法
        Method m=c.getMethod("a",int.class);

        获得指定构造方法
        Constructor ctt=c.getConstructor(int.class,String.class,float.class);
        调用构造函数 创建对象
        c.newInstance(4,"张三",38.9f);


        javaweb项目,注册的时候,表单提交一个User信息
        1.String username=request.getParameter("username");
        2.String password=request.getParameter("password");

        3.Map<String,String[]>map=request.getParameterMap();

        Map map
        ====>User

        request.setCharactorEncoding("UTF-8");
        response.setCharactorEncoding("UTF-8");


    1.步骤,创建动态web项目
    2.创建RegisterServlet(urlPattern="/register")继承HttpServlet,重写doGet和doPost,所有请求交给doGet方法处理
    3.修改index.jsp文件,添加一个表单。注册User(Stirng username,String password,String realname,String tel)

关键代码:

<form method="post" action="${pageContext.request.contextPath}/user?method=register">
    <div><input type="text" name="username" placeholder="账号"></div>
    <div><input type="password" name="password" placeholder="密码"></div>
    <div><input type="text" name="realname" placeholder="姓名"></div>
    <div><input type="text" name="tel" placeholder="电话"></div>
    <div><input type="submit" value="注册"></div>
</form>
    4.创建Userservlet处理所有的和账号密码相关的请求
        /user?method=register
<form method="post" action="${pageContext.request.contextPath}/user?method=register">
</form>

    5.在UserServlet先读取到mehtod的值,知道用户的意图
        //先读取method
        String m=req.getParameter("method");
//        根据m来找到对应的方法
        if (m!=null){
            try {
                //通过用户传递过来的method参数找到对应的方法
                Method method=this.getClass().getDeclaredMethod(m,HttpServletRequest.class,HttpServletResponse.class);
                method.invoke(this,req,resp);

    6.使用反射在当前类中找到对应的方法处理该请求
    7.提取公共类

    前端servlet问题的解决方案
    1.继承BaseServlet
    2.servlet.service
    3.不在设置单独请求映射,在请求中添加方法参数,根据方法参数,找到对应的函数处理请求
    4.反射,通过函数的名称和函数的参数类型找到方法,并且invoke该方法
    5.继承,当一个类继承BaseServlet之后,BaseServlet中的共有方法就直接可以被子类调用

两个关键代码

package co.goho.yuanyu.demo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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;
import java.util.*;

@WebServlet(urlPatterns = "/user")
//注册  /user?method=register
//登陆  /user?method=login
//退出  /user?method=logout
//读取到method参数的值,就可以知道用户的意图
public class UserServet extends BaseServlet {

    public void register(HttpServletRequest req,HttpServletResponse resp){
        Map<String, String[]> parameterMap = req.getParameterMap();
        Set<String>set=parameterMap.keySet();
        Iterator<String>it=set.iterator();
        while (it.hasNext()){
            System.out.println(it.next());
        }

    }
    public void showUsers(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        List<String> strings=new ArrayList<String>();
        strings.add("一二三");
        strings.add("456");
        strings.add("9981");
        req.setAttribute("strings",strings);
        req.getRequestDispatcher("show.jsp").forward(req,resp);
    }
}

package co.goho.yuanyu.demo;

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{
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先读取method
        String m=req.getParameter("method");
//        根据m来找到对应的方法
        if (m!=null){
            try {
                //通过用户传递过来的method参数找到对应的方法
                Method method=this.getClass().getDeclaredMethod(m,HttpServletRequest.class,HttpServletResponse.class);
                method.invoke(this,req,resp);
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值