I. Data transfer between JS and JSP
.jsp gets the js value
Because JSPS are essentially servlets, they run in a server. So you can’t get the js value directly. The client variable needs to be submitted to the server, which then receives it.
In a JSP page: (1) Set the form, or node, in a JSP (hide/not hide)
<form id="jsp_form" action="#" method="post">
<input id="jspEle" type="hidden name="jspEle">
</form>
Copy the code
(2) In js code, submit the form
<script> function setElevalue(){ var tmp = "jsEle"; document.getElementById("jspEle").value = tmp; } function submit(){var jsp_form = document.getelementById ("jsp_form"); // Get the form jsp_form.submit(); // Submit the form} </script>Copy the code
(3) After the form is submitted, set the value in JSP. At this point you get the value of the variable in JS
The < %String jsP_js = request.getParameter("jspEle"); // jspEle is the hidden control name% >Copy the code
(2).js gets the value of JSP
You can use variables in the server, such as Request.
<%
int num = 5;
request.setAttribute("num", num); % >var num = <% = request.getAttribute("num") % >; //5 num = ${requestScope.num}; / / 5Copy the code
Two. Data transfer between JSP and servlet
(1) JSP data into the servlet
1. Method 1: Asynchronous request Using Ajax, send asynchronous requests and send data to servlets. (For asynchronous requests, you can also write parameters in the URL.)
$.post("ajaxServlet", {username:"jack"},function(data){
alert(data);
},"text"); // If type is not set, it is intelligent by default
Copy the code
When submitting a form, you can serialize the form. Example: Submit a login form
$("#login_form").submit(function () {
// Send an asynchronous request
$.post("userServlet", $(this).serialize(), function (data){})})Copy the code
2. Method 2: Directly submit the form and set the action for the form
<form id="regist_form" method="post" action="userServlet" >
<input id="rname" type="text" name="uname" placeholder="Username" ">
<input id="rpassword" type="password" name="password" placeholder="Password" />
<input id="rsubmit" type="submit" value="Registered"/>
</form>
Copy the code
(2) Servlet data into JSP
1. Method 1: Store variables in a domain, such as session and cookie.
request.getSession.setAttribute("user",user);
Copy the code
You can also save a request and then forward it to access a JSP
request.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
Copy the code
Method 2: Return data in an asynchronous request Note: The returned data type must be the same as that preset for an asynchronous request. Return JSON data:
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
response.setContentType("application/json; charset=utf-8");
response.getWriter().write(json);
Copy the code
(three) JSP processing servlet incoming data
1. You can directly obtain the values stored in the Request session
User user = (User)request.getAttribute("user");
Copy the code
2. The data returned in the asynchronous request can be obtained directly from the returned data data. For jSON-type data, use the method of getting JSON data.
(4) Servlet processing JSP incoming data
1. Parameters passed in the asynchronous request
String id = request.getParameter("id");
Copy the code
For an asynchronous request to submit a form: Use the BeanUtils utility class to encapsulate it in an object (note: the name of the form must be the same as the variable name in the object)
// Get parameters
Map<String, String[]> map = request.getParameterMap();
// Encapsulate parameters
User registUser = new User();
try {
BeanUtils.populate(registUser, map);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Copy the code
2. Parameters set in the URL
String params = request.getQueryString();
Copy the code
Add: The problem of garbled Chinese characters when receiving data can be solved by using this statement: (usually the user input data in the page)
String title = request.getParameter("title");
title = new String(title.getBytes("iso-8859-1"),"utf-8");// Of course utF-8 can be changed to GBK, Unicode
Copy the code
Data transfer between JSP and JSP
You can use location.href to jump from JSP to JSP. Pass parameters in the URL. Ex. :
location.href = "index.jsp? id="+ 1 +"";
/ / in the index. The JSP
var id = ${param.id};
Copy the code
Data transfer between HTML and servlet
(1) HTML data is passed into the servlet
1. Approach 1: Use the href format of the A tag: through the hyperlink? + The name of the attribute to be passed = the value of the attribute. Multiple attributes need to be concatenated with &
<a href="userServlet? username=jack&password=123"></a>
Copy the code
2. Method 2: Use Ajax (JSP and Servlet data interaction section explained)