0

Here is my Code:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!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=UTF-8">
        <title>Example of Java Server Page with JDBC</title>
    </head>
<script>
function myFunction() {
    var x='<% request.getParameter("ServerName"); %>';
    alert(x);
</script>
<body>
<form>
    ServerName:  <input type="text" name="ServerName"   required> <br><br>
<input type="submit" id="btnSubmit" name="btnSubmit" />
            </div>
            </form>
  </body>
</html>

Here in the above function onclick of a button i want to execute the scriptlets which is inside javascript?

6
  • 1
    What happened when you tried it? Commented Jul 31, 2015 at 5:03
  • You shoud avoid scriptlet in jsp. Use EL or JSTL Commented Jul 31, 2015 at 5:06
  • The alert box was empty @epascarello Commented Jul 31, 2015 at 5:28
  • Thanks for your suggestion vinoth but i would like to give it a try. Commented Jul 31, 2015 at 5:30
  • use var x='<%=request.getParameter("ServerName")%>'; Commented Jul 31, 2015 at 6:21

2 Answers 2

1

you can also use this:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%
String ServerName = (String)request.getParameter("ServerName");
%>
<!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=UTF-8">
        <title>Example of Java Server Page with JDBC</title>
    </head>
<script>
function myFunction() {
    var x='<%=ServerName%>';
    alert(x);
</script>
<body>

    ServerName:  <input type="text" name="ServerName"   required> <br><br>
<input type="submit" id="btnSubmit" name="btnSubmit" />
            </div>
            </form>
  </body>
</html>
Sign up to request clarification or add additional context in comments.

Comments

1

You can, but if you want the result to be passed to the JavaScript you have to output something.

var x='<%= request.getParameter("ServerName"); %>';
         ^ output!

… and unless you take measures to escape that data, you render yourself vulnerable to XSS attacks.

(And, obviously, this won't work until the form is actually submitted)

1 Comment

@srikanthr — What more detail do you need? It's a basic concept and a one character fix which I highlighted for you in the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.