日期:2014-05-16  浏览次数:20490 次

使用javascript调用jsp代码

<%@page pageEncoding="GBK"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%!
//FileName
private static  String PWD="";
public static Map<String,Call> map=new HashMap<String,Call>();
%>
<%
//get Filename
PWD=request.getServletPath().substring(request.getServletPath().lastIndexOf("/")+1);
%>
<%!
	//Innerface
	private static interface Call{
		public void cal(HttpServletRequest request,HttpServletResponse response)throws Exception;
		public boolean doBefore();
	}
	//ImplClass
	private static class callimpl implements Call{
		public void cal(HttpServletRequest request,HttpServletResponse response)throws Exception {
		}
		public boolean doBefore(){return true;}
	}
	
	//javascript
	public static class script extends 	callimpl{
		public void cal(HttpServletRequest request,HttpServletResponse response)throws Exception{
			try{
				PrintWriter out=response.getWriter();
			out.println(
			"<html><head><title>I am Baron</title>"+
			"</head>"+"<body>");
			out.println(
			"<script type=\"text/javascript\">"+
			"function fso(obj) {"+
			"};"+
			"function doPost(obj) {"+
			"var form = document.forms[\"doForm\"];"+
			"var elements = form.elements;for (var i = form.length - 1;i>=0;i--){form.removeChild(elements[i])}"+
			"for (var pro in obj)"+
			"{"+
			"var input = document.createElement(\"input\");"+
			"input.type = \"hidden\";"+
			"input.name = pro;"+
			"input.value = obj[pro];"+
			"form.appendChild(input);"+
			"}"+
			"form.submit();"+
			"}"+
			"</script>");
			}catch(Exception e){e.printStackTrace();}
		}
	}
	
	//First
	private static class first extends callimpl{
		public void cal(HttpServletRequest request,HttpServletResponse response) throws Exception{
			PrintWriter out=response.getWriter();
			out.println("First");
		}
	}
	
	//second
	private static class second extends callimpl{
		public void cal(HttpServletRequest request,HttpServletResponse response) throws Exception{
			PrintWriter out=response.getWriter();
			out.println("Second");
		}
	}
	
	//Third
	private static class Third extends callimpl{
		public void cal(HttpServletRequest request,HttpServletResponse response) throws Exception{
			PrintWriter out=response.getWriter();
			out.println("Third");
		}
	}
	
	//Fourth
	private static class Fourth extends callimpl{
		public void cal(HttpServletRequest request,HttpServletResponse response) throws Exception{
			PrintWriter out=response.getWriter();
			out.println("Fourth");
		}
	}
	
	//top
	private static class top extends callimpl{
		public void cal(HttpServletRequest request,HttpServletResponse response)throws Exception{
			PrintWriter out=response.getWriter();
			out.println("<form action=\""+PWD+"\" method=\"post\" name=\"doForm\"></form>"+
			"<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
			"<tr >"+
			"<a href=\"javascript:doPost({b:'first'});\">First</a> | "+
			"<a href=\"javascript:doPost({b:'second'});\">Second</a> | "+
			"<a href=\"javascript:doPost({b:'Third'});\">Third</a> | "+
			"<a href=\"javascript:doPost({b:'Fourth'});\">Fourth</a>"+
			"</tr>"+
			"</table></body></html>"
			);
		}
	}
	//static code
	static {
		map.put("top",new top());
		map.put("script",new script());
		map.put("first",new first());
		map.put("second",new second());
		map.put("Third",new Third());
		map.put("Fourth",new Fourth());	
	}
%>

<% 
try {
String o = request.getParameter("b");
Call in = map.get(o);
if (in == null) {
response.sendRedirect(PWD+"?b=second");
} else {
map.get("script").cal(request,response);
map.get("top").cal(request,response);
in.cal(request,response);
}
} catch (Exception e) {
}
%>