一套完整且古老的web前端和后端 数据传递 流程(JSP和Servlet及session来实现的)

1、前台代码test.jsp提交:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println("path="+path);
System.out.println("basePath="+basePath);

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    This is my JSP page. <br>
    action的提交路径一定要加上项目名称,否则404。即 <%=basePath %>servlet/WebReturnTest<br>
    
	<form id="opForm" action="<%=basePath %>servlet/WebReturnTest" method="POST">
	  <table border="0">
	    <tr>
	      <td>
	        <input name="username" value=""/>
	        <input type="button" name="" value="click" onclick="doSubmit()"/>
	      </td>
	    </tr>
	  </table>
	</form>

  </body>
  <script type="text/javascript">
  function doSubmit(){
    document.forms[0].submit();//提交
  }
  
  function doWhat(){
  	if(window.clipboardData) {
		//清空剪切板
		window.clipboardData.clearData();
		//复制
		window.clipboardData.setData("Text", returnValue);
  	}
  }
  </script>
</html>

2、web.xml配置:

<servlet>
	<description>This is the description of my J2EE component</description>
	<display-name>This is the display name of my J2EE component</display-name>
	<servlet-name>WebReturnTest</servlet-name>
	<servlet-class>com.ly.tetsWeb.WebReturnTest</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>WebReturnTest</servlet-name>
	<url-pattern>/servlet/WebReturnTest</url-pattern>
</servlet-mapping>

3、后台接收代码:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class WebReturnTest extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public WebReturnTest() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("-----------GET------");
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out
//				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the GET method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		System.out.println("--------POST---------");
		HttpSession session = request.getSession(false);
		session.setAttribute("username", "99");
		request.getRequestDispatcher("/test2.jsp").forward(request, response);
		
//		response.setContentType("text/html");
//		PrintWriter out = response.getWriter();
//		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
//		out.println("<HTML>");
//		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
//		out.println("  <BODY>");
//		out.print("    This is ");
//		out.print(this.getClass());
//		out.println(", using the POST method");
//		out.println("  </BODY>");
//		out.println("</HTML>");
//		out.flush();
//		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

4、即将跳转的页面test2.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
     前台html/jsp如何获得后台session的值? <br>
     通过    var username = "${username}";    接收后台提交过来的session数据 <br>
     111----${username}-----222
  </body>
 
</html>

5、阿里开发手册要求:这一点不是很明白。参考我的链接:阿里Java开发手册的利用,谨防忘记!

猜你喜欢

转载自blog.csdn.net/qq_37358143/article/details/98749231