JSP and interactive data and special characters JavaScirpt handling

2010-12-13  来源:本站原创  分类:Web  人气:138 

Data exchange data and JavaScirpt JSP

JSP data and JavaScirpt the use of a data exchange solution for the WEB program, the front-end (JavaScript) and back-end (JSP / Servlet) is no way to share data, only the back-end program (JSP) to data output

To generate pages to the front, this time the page generated JavaScript code in the jsp it be possible to get the so-called data. Similarly, only the JavaScript in the data to the back-end JSP code, JSP JavaScript program to get the data.

Then how to use JavaScript in the page data in jsp or in jsp page using JavaScript in the data?

First, the page data in JavaScript how to submit to the background of the jsp program

① data can be JavaScript to xxx.JSP? Var1 = aaa & var2 = bbb in the form of a URL parameter passed to JSP program, when used in jsp

<% String strVar1 = request.getParameter ("var1 ");%> can get to the JavaScript script to pass over the data;

② By using JavaScript to add a hidden field in the form of information, then submit the form the way the data is passed to the JSP program.

Refer to the following script:

<script language="JavaScript">

/************************************************* **************

* JavaScript script, placed anywhere in the page can

* Insertclick () function to get the JSP page is passed to the variable varMC,

* You can then modify the JavaScript value of this variable, and then through

* Post the submission to the JSP program to use.

************************************************** *************/

function insertclick () {

var1 = document.all.mc.value; / / Get the variable value in page form

var1 = var1 + "name";

document.insertForm.submit ();


</ Script>

<! - Html form page form, placed in the position of the Open html page ->

<form name="insertForm" method="post" action="yourJSP">

<! - The following sentence is to get the JSP program that passed over the variable value ->

<Input type = "hidden" name = "mc" value ="<%= varMC%> ">

<input type="button" value=" Submit ">

</ Form>

Second, the page data in JavaScript how to use the data in the background of the JSP program

This relatively simple scripts directly in JavaScript using <% = strVar1%> jsp program to connect to the data passed to the JavaScript script to use.

Commonly used expression is assigned to a JS script variable, but the contents of this situation when the expression contains special characters (such as: "'\ r \ n, etc.), the JS will not resolve the error down the execution. reasons: such as content in double quotes, it will contain expressions with the most outside the double quotes confusion will result in quotes does not match the situation to the implementation of the final error JS off, another example: If the expression in \ r When, JS variable value is not obtained the same line, when JS is not often under the implementation, of the form: If the expression says: abcdef \ r ghilm

var serverToPage = "<% = request.getAttribute (" showContent ")%>";

If the expression of a turning, the actual page is:

var serverToPage = "abcdef

ghilm ";

So error, and suspended execution. But you can use the following special characters in the second approach the situation.

1. With a script assigned to a JS variable expressions: the expression should be enclosed in double quotes such as:

<script type="text/javascript">

var string = "<% = request.getAttribute (" userName ")%>"

The statement in the script does not need quotation marks:

<%! String name = "sasa"%>

<script type="text/javascript">

function land () {

var aaa ="<%= request.getAttribute ("userName ")%>"; when the userName is empty judged as empty as: if (aaa ==" null "){//...}

var bbb = "$ {userName}"; / / use the quotation marks are in scope, otherwise not get out, such as: "$ {userName}"

var ccc = <% = name%>; / / declared in the script does not need quotation marks:

alert (aaa);

alert (bbb);

alert (ccc);


</ Script>

2. First expression is displayed in a hidden HTML tags, such as: <div> </ div>, <h> </ h> and so on. JS code and then use the value from the hidden label to show in the final head to ground.

index.jsp page:

<form action="txtServlet" method="post">
<textarea rows="10" cols="30" name="txtContent"> </ textarea>
<textarea rows="10" cols="30"> </ textarea>
<h1> content: <% = request.getSession (). getAttribute ("contentString")%> </ h1>
<input type="submit" value=" Submit "/>
</ Form>
</ Body>
</ Html>
<script type="text/javascript">
var node = document.getElementById ("showContent");
var tempNode = document.getElementById ("txtTemp");
node.value = tempNode.innerHTML;
</ Script>

txtServlet code:

String txtString = request.getParameter ("txtContent");
request.getSession (). setAttribute ("contentString", txtString);
response.sendRedirect ("index.jsp");

  • JSP and interactive data and special characters JavaScirpt handling 2010-12-13

    Data exchange data and JavaScirpt JSP JSP data and JavaScirpt the use of a data exchange solution for the WEB program, the front-end (JavaScript) and back-end (JSP / Servlet) is no way to share data, only the back-end program (JSP) to data output To

  • HTML.JS.JSON special characters 2010-12-13

    JS to convert HTML special characters These days to do the project, found that in JSON format from the server to pass data to the client, by JS displayed in HTML pages, there are some special characters can not be directly displayed as the background

  • Handling special characters in the data submitted 2010-04-06

    Users can not log on to reflect recent new system, view the background is the password verification did not pass, but the user that the password to log on the old system, and made us puzzled. With debugging information, it discovered that many of the

  • Escape special characters 2010-06-13

    Transfer from (http://xhfei.javaeye.com/admin/blogs/new) # Spring not only provides a full-featured application development framework, itself also has a number of programming when you can use the tools directly, you can not only apply in the Spring c

  • Replace special characters java 2010-04-14

    Today in the jsp page in js calendar display with a special character string, the string end of the reporting error, use replaceAll ("\ \ s +", "") can replace the special characters, replacing the previous statement spaces, and can al

  • Java filter regular expression special characters reserved ---- 2010-04-22

    / / Filter special characters public static String StringFilter (String str) throws PatternSyntaxException ( / / Allow only letters and numbers / / String regEx = "[^ a-zA-Z0-9]"; / / Clear out all the special characters String regEx ="[`~!

  • java url in dealing with special characters 2010-05-13

    java url in dealing with special characters (such as &,%...) URL (Uniform Resoure Locator, Uniform Resource Locator) is the Internet in a unified resource location and management of the signs. A complete URL, including the following: 1. Application p

  • sql special characters handling 2010-05-14

    User input without any restrictions, then the special characters must be transformed. If single quotes are not transformed, a database error occurs and may even cause the system to crash. But avoid the method is very simple, as long as the single quo

  • In the generated html or xml document, the document should take note of special characters 2010-06-16

    / / In the generated html or xml document the document to take note of special characters, otherwise an error. HTML escape character in HTML, <,>, &, etc. have special meaning, (the first two characters for the link to sign, & for escape), n

  • SQL on the basic method of handling special characters 2010-07-13

    Must be entered on the user, such as quotes, angle brackets and other special characters to give adequate attention, they may cause serious security problems. SQL injection is one of the basic approach is to use single quotation marks unfiltered secu

  • xml "special characters" 2010-08-20

    Problems encountered in this area because I tried to serialize an object after the storage to the xml file, and then read the string from the xml file and deserialize objects (hadoop mapreduce program transmitted through JobConf object to tasktracker

  • URL handling of special characters 2010-09-23

    Transfer from http://www.jb51.net/article/21691.htm Hex value 1. + URL in the space% 2B + sign indicates 2. URL spaces in space can be numbers or codes% 20 + 3. / Separate directory and subdirectories% 2F 4.? Separated the actual URL and the paramete

  • Special characters in batch 2010-11-26

    Special symbols used 1, @ command line Shield character echo 2,% batch variables leading char 3,> redirection operator 4,>> redirection operator 5 ,<,>,<& redirector 6, | command pipe character 7, the ^ escape character 8, the combin

  • JS processing xml special characters 2010-11-26

    Xmlhttp using cell sheet this week, with multiple rows of data components into the development of an asynchronous operation, encountered data & xml parsing error occurred Reflector and google and found that due to special characters causing the need

  • oracle password special characters in use in imp.exp 2011-05-09

    oracle password special characters in the imp, exp in the use of ORACLE 2010-06-12 11:46:15 read 156 comments 0 Font Size: Large Medium Small Subscribe Data export to do today, because of the user name password using special characters, so encountere

  • oracle insert special characters '&' problem 2011-05-09

    Initialized data in the database, insert special characters encountered problems, such as a menu table: id, name, url. Insert data: insert into MENU values ​​('1 ',' product type ',' / productType.do? method = list & type = 01 '); Because of the spec

  • RAQ show the flexibility of handling special characters 2011-05-05

    In web applications, often encountered in the treatment of some special characters, such as to display such characters "" in Html, word, and java in a separate character in this show is very simple. But when the three conditions required in this

  • Oracle how to insert special characters: & and '(multiple solutions) (rpm) 2011-08-01

    Oracle how to insert special characters: & and '(multiple solutions) Today, a number of importing data to Oracle, ran into a problem: Toad Tips AMP to give a custom variable assignments at the beginning I was wondering, the data is a series of Insert

  • Oracle bulk insert special characters, when the problem 2011-08-09

    Bulk insert data, because of some special characters cause data to not insert data In the insert script using the following statement can close the special character escaping the Oracle: set define off;

  • Oracle escape some special characters 2011-08-09

    Reproduced records do note: SQL> select * from test; TEST -------------------- sdd_kk d'd dfsfsa dffa% asfs 12345 1% 2345 1% 54321 2% 54321 %% 54321 A & B 9 rows selected. Which contains special characters are %,_,&, may contain these character