1.jsp->java(编码)->java
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> <script type="text/javascript" src="jquery-1.9.1.js"></script> </head> <body> <a onclick="testWbAPI()">share</a> <script> function testWbAPI(){ //var url = "http://127.0.0.1:6080/helloworld/receiveWBAPI?userId=111&content="+encodeURI("我哦我我"); var url = "/myweb/ShareWBAPI?userId=111&content="+encodeURI("我哦我我"); window.open(url); /*$.ajax({ type: "POST", //url: "/myweb/ShareWBAPI", url:"http://127.0.0.1:6080/helloworld/receiveWBAPI", data: "userId=11111", success: function(msg){ alert( "Data Saved: " + msg ); }, error:function(x,a,s){ alert(x+a+s) } }); */ } </script> </body> </html>
package bookstore; import java.io.IOException; import java.net.URLEncoder; import java.util.Calendar; import java.util.GregorianCalendar; 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 org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.PostMethod; /** * Servlet implementation class ShareWBAPI */ @WebServlet("/ShareWBAPI") public class ShareWBAPI extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShareWBAPI() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String POST_URL="http://127.0.0.1:6080/helloworld/receiveWBAPI"; response.sendRedirect(POST_URL+"?userId=111&content="+URLEncoder.encode(new String(request.getParameter("content").getBytes("iso-8859-1"),"utf-8"), "gbk")); } }
package test; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class receiveWBAPI */ public class receiveWBAPI extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public receiveWBAPI() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/shareWbAPI.jsp").forward(request, response); } }
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!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=GB18030"> <title>Insert title here</title> </head> <body > <a onclick="showPara()">share</a> <div>userId<%=request.getParameter("userId") %></div> <div>content<%=new String(request.getParameter("content").getBytes("iso-8859-1"),"gbk") %></div> </body> </html>
2.jsp->Ajava->Bjava(sendRedirect,实际未跳转)->Ajava(跳转)
Ajava
package bookstore; import java.io.IOException; import java.net.URLEncoder; import java.util.Calendar; import java.util.GregorianCalendar; 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 org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; /** * Servlet implementation class ShareWBAPI */ @WebServlet("/ShareWBAPI") public class ShareWBAPI extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ShareWBAPI() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String POST_URL="http://127.0.0.1:6080/helloworld/receiveWBAPI"; // URL postUrl = new URL(POST_URL); // HttpURLConnection connection = (HttpURLConnection) postUrl // .openConnection(); // connection.setDoOutput(true); // connection.setDoInput(true); // connection.setRequestMethod("POST"); // connection.setUseCaches(false); // connection.setInstanceFollowRedirects(true); // connection.setRequestProperty("Content-Type", // "application/x-www-form-urlencoded"); // // connection.setRequestProperty(key, value) // /**//* // * 与readContentFromPost()最大的不同,设置了块大小为5字节 // */ // connection.setChunkedStreamingMode(5); // connection.connect(); // // /**//* // * 注意,下面的getOutputStream函数工作方式于在readContentFromPost()里面的不同 // * 在readContentFromPost()里面该函数仍在准备http request,没有向服务器发送任何数据 // * 而在这里由于设置了ChunkedStreamingMode,getOutputStream函数会根据connect之前的配置 // * 生成http request头,先发送到服务器。 // */ // DataOutputStream out = new DataOutputStream(connection // .getOutputStream()); // String content = "userId=11111&content=" + URLEncoder.encode("一个大肥人", "utf-8"); // out.writeBytes(content); // // out.flush(); // out.close(); PostMethod postMethod = new PostMethod(POST_URL); postMethod.addParameter("userId","1111"); postMethod.addParameter("content","主题"); HttpClient client = new HttpClient(); client.setConnectionTimeout(1000 * 60); int status=0; try { status = client.executeMethod(postMethod); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(status==HttpStatus.SC_OK) { } else { } postMethod.releaseConnection(); //因为Bjava,redirect所以返回302, if ((status == HttpStatus.SC_MOVED_TEMPORARILY) ||(status == HttpStatus.SC_MOVED_PERMANENTLY) ||(status == HttpStatus.SC_SEE_OTHER) || (status == HttpStatus.SC_TEMPORARY_REDIRECT)) { //读取新的URL地址 Header header = postMethod.getResponseHeader("location"); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals(""))) newuri ="/"; GetMethod redirect = new GetMethod(newuri); client.executeMethod(redirect); Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+newuri); //跳转页面效果 System.out.println("Redirect:"+ redirect.getStatusLine().toString()); redirect.releaseConnection(); } else System.out.println("Invalid redirect"); } } }
Bjava
package test; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class receiveWBAPI */ public class receiveWBAPI extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public receiveWBAPI() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("/helloworld/shareWbAPI.jsp"+"?userId"+request.getParameter("userId")+"&content="+URLEncoder.encode(new String(request.getParameter("content").getBytes("iso-8859-1"),"gbk"),"gbk")); } }
时间: 2024-10-09 19:24:52