Servlet第九节整理

1.动态运行反射机制

//主业务设计
package com.entity;

public interface Sale {
	void doSale();

}

//辅助业务设计
package com.entity;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
// 非业务功能在类Peisong中实现
public class Peisong implements InvocationHandler {
//	client代表可能出现的各种客户类
	private Object client;
		
	获得客户类对象
//	public void setClient(Object client) {
//		this.client = client;
//	}
	public Peisong(Object client){
		this.client=client;
	}
	@Override
	public Object invoke(Object proxy, Method arg1, Object[] arg2) throws Throwable {
		// TODO Auto-generated method stub
		//非核心业务方法
		System.out.println("开始配送");
		//执行客户方的核心业务
		arg1.invoke(client, arg2);
		//非核心业务方法
//		物流公司的非核心业务方法
		System.out.println("已经送到,配送结束");
	return null;
	}

}

//测试类
package com.entity;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

public class TestPeisong {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		BookStore b=new BookStore();
		InvocationHandler ih=new Peisong(b);
		
		
//		动态创建代理类对象
		Sale myproxy=(Sale) Proxy.newProxyInstance(b.getClass().getClassLoader(), b.getClass().getInterfaces(), ih);

//		执行代理者的方法
		myproxy.doSale();
		
		Manufacture m=new Manufacture();
		InvocationHandler ih2=new Peisong(m);
		
		myproxy=(Sale) Proxy.newProxyInstance(m.getClass().getClassLoader(), m.getClass().getInterfaces(), ih2);
		myproxy.doSale();
		

	}

}

2.ajax

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function test(){
	//建立ajax请求对象
	var xhr=new XMLHttpRequest();
	//打开那个地址上的请求资源,使用什么方式 同步还是异步
	xhr.open('get','test.do',true);
	//发送出去
	xhr.send();
	//请求提出之后的状态(0初始化状态 1开始发送请求 2发送结束 3响应解析 4客户端准备就绪进行输出)
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4&&xhr.status==200){
			//客户端输出的文本
			var data=xhr.responseText;
			//对应标签
			var p1=document.getElementById('p1');
			//放在文本里
			p1.innerText=data;
		}
	}
}
function checkUid(){
	var uid=document.getElementsByName('uid')[0].value;
	var xhr=new XMLHttpRequest();
	var tip=document.getElementById('tip');
	xhr.open('get','checkuid.do?uid='+uid,true);
	xhr.send();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4&&xhr.status==200){
			var result=xhr.responseText;
			if(result=="suc"){
				tip.innerText='用户名已经存在,请重新输入';
			}else{
				tip.innerText='用户名合法';
			}
		}
	}
}
</script>
</head>
<body>
	<input type='button' value='测试ajax' onclick='test()'><br>
	<p id='p1'></p>
	<input type='radio' name='gender' /><input type='radio' name='gender' /><form>
	<input type='text' name='uid' placeholder='注册用户ID' onblur="checkUid()" /><span id='tip'></span>
	<br>
	<input type='text' name='uname' placeholder='用户真实姓名' />
	<br>
	</form>
</body>
</html>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String uid=request.getParameter("uid");
//		从数据库查询步骤暂时省略
//		假定查到一个数值rong
		PrintWriter pw=response.getWriter();
		if(uid.equalsIgnoreCase("rong")){
			pw.print("suc");
		}else{
			pw.print("fail");
		}
		pw.close();
	}

3.使用ajax之后 servlet不能使用请求转发 没有用的

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script>
function log(){
	/* 取消提交按钮的默认提交事件 */
	event.preventDefault();  
	var uid=document.getElementsByName('uid')[0].value;
	var pwd=document.getElementsByName('pwd')[0].value;
	var url="uid="+uid+"&pwd="+pwd;
	var xhr=new XMLHttpRequest();
	xhr.open('post','log.do',true);
	xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xhr.send(url);
	
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4&&xhr.status==200){
			var data=xhr.responseText;
			/*使用了Ajax后,不能再使用Servlet通过请求转发跳转到主页面 */
			if(data=="fail"){
				alert('用户名或密码错误');
			}else{
				window.location.href='main.jsp';
			}
		}
	}
	
	
}

</script>
</head>
<body>
	<form  method='post'>
	<input type='text' name='uid' placeholder='用户ID'  />
	<br>
	<input type='text' name='pwd' placeholder='用户密码' />
	<br>
	<input type='submit' value='登录' onclick='log()' />
		
	</form>
</body>
</html>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String uid=request.getParameter("uid");
		String pwd=request.getParameter("pwd");
		if(uid.equalsIgnoreCase("admin")&&pwd.equalsIgnoreCase("123")){
			HttpSession hs=request.getSession();
			hs.setAttribute("uid", uid);
			
		}else{
			PrintWriter pw=response.getWriter();
			pw.print("fail");
		}
	}

4.json 初次前后端分离的代码(需要使用json架包)

//main.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script>
function query(){
	var xhr=new XMLHttpRequest();
	xhr.open('get','querysingle.do',true);
	xhr.send();
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4&&xhr.status==200){
			/*strjson是从服务器传过来的json字符串  */
			var strjson=xhr.responseText;
			/* 利用JSON.pare()把json解析成对象*/
			var person=JSON.parse(strjson);
			/* 假定人类类型对象有姓名和年龄两个属性*/
			var p1=document.getElementById('p1');
			p1.innerText='人的姓名:'+person.name+',人的年龄:'+person.age;	
		}
	}	
}
</script>
</head>
<body>
<p>${sessionScope.uid}你好,欢迎登录</p>
<p><input type='button' value='查询' onclick='query()' /></p>
<p id='p1'></p>
<div>
<table id='t1'>
</table>
</div>
</body>
</html>
//text servlet
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//		假定这是从后台数据库查询到结果后封装出来的对象
		Person p1=new Person("曹操",60);
//		把对象转换成JSON字符串
		Gson g=new Gson();
		String pstr=g.toJson(p1);
		System.out.println(pstr);
//		把JSON字符串传递传递到前台
		response.setCharacterEncoding("utf-8");
		PrintWriter pw=response.getWriter();
		pw.print(pstr);
		pw.close();
		
	}
//Person类
package Ajxa;

public class Person {
	private String name;
	private int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值