I have this code:
<script type="text/javascript">
var endpoint = "http://localhost:8080/LWP/in.jsp?code=" + "<% out.println(request.getParameter("code")); %>";
window.opener.location.href = endpoint;
window.close();
</script>
What I expect it to do is redirect the page which opened the browser window this is processing in to
http://localhost:8080/LWP/in.jsp?code=<code here>
If I remove the <% out.println() %> portion of the script, it works fine, and I am redirected as expected (minus the value being passed in).
What am I doing incorrectly with the output of the parameter?
I also tried removing out.println. Still does not work.
Figured it out. Had to assign the value to a new variable, then use that.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<% String code = request.getParameter("code"); %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
var code = "<%=code%>";
var endpoint = "http://localhost:8080/LWP/in.jsp?code=" + code;
window.opener.location.href = endpoint;
window.close();
</script>
</head>
<body>
</body>
</html>
Related
Here is a sample jsp page with a form.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="h.abc" %>
<%
abc p = new abc();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="abcd.jsp" method="POST">
<input type="hidden" name="<%=p.getName()%>+'Name'" value='y'>
<input type='text' name='<%=p.getName()%>' >
<input type='submit' value='submit'>
</form>
</body>
</html>
Here is the abc class in the 'h' package.Please pardon the naming.It's only for illustration purposes.
public class abc {
public String name="abc";
public abc()
{
}
public String getName()
{
return name;
}
}
And this is the abcd.jsp target page.Here i am trying to get the value of the input field.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="h.abc" %>
<%
abc p = new abc();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String s = p.getName();
out.println(request.getParameter(s+"Name"));
out.println(request.getParameter(s));
%>
</body>
</html>
I am unable comprehend why the input field is not returning 'y' as its value.rather it is returning null.
change the code in first jsp
<input type="hidden" name="<%=p.getName()%>Name" value='y'>
you need put your abc instance in HttpServletRequest'attributes by calling setAttribute("you_key", abcInstance) method. And then you can get abc instance in your jsp pages by calling req.getAttribute("your_key") or using el expression
Login Form (index2.html)
<html>
<head>
<title>Login Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body bgcolor="96D2C2">
<form name="ask_1" method="get" action="index24.jsp">
Username: <input type="text" name="id11"/> <BR>
Password: <input type="password" name="id22" /> <BR>
<input type="submit" value="Login" /> <BR>
<BR>
</form>
Create an Account
</body>
My JSP code in order to verify the login info and print to user success or not... (index24.jsp)`
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#page import="java.sql.*" %>
<% int j=0; %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
String myDatabase = "jdbc:mysql://localhost:3306/mydb1?user=root&password=1234";
Connection myConnection = DriverManager.getConnection(myDatabase);
Statement myStatement = myConnection.createStatement();
String id11=request.getParameter("id11");
String id22=request.getParameter("id22");
String sqlString = "SELECT FROM users WHERE username='"+id11+"' AND password='"+id22+"' ";
ResultSet rs = myStatement.executeQuery(sqlString);
if(rs.next()) {
System.out.println("Success"); }
else {
System.out.println("Failed");
}
%>
</body>
</html>
The code given above is giving me the error, "The requested resource is not available.". Any suggestions and improvements to my code or edit are appreciated.
the Connection myConnection = DriverManager.getConnection(myDatabase);
is kind wrong because the getConnection() will not handle the url given as parameter String try to do it like this :
Connection myConnection = DriverManager.getConnection("jdbc:mysql://localhost/mydb1" , "root" , "1234");
you can check the documentation of that method , and dont'forhet to remove this line
String myDatabase = "jdbc:mysql://localhost:3306/mydb1?user=root&password=1234";
Try to close the "try {" block with
} catch (Exception e) {
e.printStackTrace(new java.io.PrintWriter(out));
}
to get more details about the error. It is a simple trick for developing jsps and should be removed in an official version of a web-site.
Also, it may be out of topic, but your code is susceptible to SQL injection. See more here
http://www.w3schools.com/sql/sql_injection.asp
Finally the problem was the server (GlassFish)... i used Apache Tomcat and works properly...
This is what i have, when i run the jsp file on the browser the only thing i get is the "Report" header but not the query result, please guide me to the right way.
<%#page import="java.sql.ResultSet"%>
<%#page import="database.Dba"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<center> <h1> Report </h1></center>
<%
//scriplet
try{
Dba db =
new Dba(application.getRealPath("daw.mdb"));
db.conectar();
db.query.execute("select usuario from usuarios");
ResultSet rs = db.query.getResultSet();
while(rs.next()){
out.print(rs.getString(1)+ "<br>");
}
db.desconectar();
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
I am very new at "jsp" and "jquery", I think the code below should display a number on screen and increase it by one every 3 seconds, but after 2 or 3 repetitions it breaks and starts to show wrong numbers
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#load_me').load('index.jsp').fadeIn("fast");
}, 3000); // autorefresh the content of the div after
//every 3000 milliseconds(3sec)
</script>
</head>
<body>
<%! int i = 0;%>
<div id="load_me">
<%out.print(++i);%>
</div>
</body>
</html>
I even tried to show time instead of printing a number, but the same problem accrued :
<%# page import="java.text.SimpleDateFormat" %>
<%# page import="java.util.Date" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#load_me').load('index.jsp').fadeIn("fast");
}, 3000); // autorefresh the content of the div after
//every 3000 milliseconds(3sec)
</script>
</head>
<body>
<div id="load_me">
<%
Date d = new Date();
SimpleDateFormat sp = new SimpleDateFormat("hh:mm:ss");
String t= sp.format(d);
out.print(t);
%>
</div>
</body>
</html>
it seems you want a 'div#load_me' should display a number increment of 1 every 3 seconds.. Try the following plain javaScript for the same:
setInterval((function() {
var currNumber = 0;
return function() {
document.getElementById('load_me').innerHTML = ++currNumber;
}
})(), 3000);
EDIT (To demonstrate the full code) :
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<!-- we do not need jquery any more :-) -->
</head>
<body>
<div id="load_me">
</div>
<script>
setInterval((function() {
var currNumber = 0;
return function() {
document.getElementById('load_me').innerHTML = ++currNumber;
}
})(), 3000);
</script>
</body>
</html>
And this way you can avoid the unnecessary server calls as well.
This code below works for time showing, but you have to create date.jsp page,
<%# page import="java.text.SimpleDateFormat" %>
<%# page import="java.util.Date" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.2.js"></script>
<script type="text/javascript">
var auto_refresh = setInterval(
function () {
$('#load_me').load('date.jsp').fadeIn("slow");
}, 3000);
</script>
</head>
<body>
<div id="load_me">
<%# include file="date.jsp"%>
</div>
</body>
</html>
date.jsp :
<%# page import="java.util.Date" %>
<%# page import="java.text.SimpleDateFormat" %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
</head>
<body>
<%
Date d = new Date();
SimpleDateFormat sp = new SimpleDateFormat("hh:mm:ss");
String t= sp.format(d);
out.print(t);
%>
</body>
</html>
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript">
function sendData()
{
var data={"id":101,"name":"suraj"};
var xmlhttp=new XMLHttpRequest();
var url="File1.jsp";
url = url + "?data="+data;
xmlhttp.open("GET", url, true);
xmlhttp.send();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
}
</script>
</head>
<body>
<h3> To send data through json : </h3>
<input type="button" value="sendData" onClick="sendData()"/>
<div id="myDiv">
</div>
</body>
</html>
// the javascript object has to be sended to jsp page with the help of ajax , and i have to convert that javascript object to java objects , how can i perform that task
File1.jsp
<%#page import="org.json.simple.JSONValue"%>
<%#page import="org.json.simple.JSONObject"%>
<%#page import="org.json.simple.parser.JSONParser"%>
<%
String data=request.getParameter("data");
out.println("<h3>data sended successfully <h3>"+data);
String jsonn=JSONValue.toJSONString(data);
out.println(jsonn);
JSONParser parser=new JSONParser();
JSONObject json=(JSONObject)parser.parse(jsonn);
String name=(String)json.get("name");
int id=(int)json.get("id");
out.println(id+" "+name);
%>
here i tried to parse it to json object and then convert it to java object, which is not happening ...
// I cant convert that javascript object to java variables . Help me out with the piece of code . what should i do to convert the javascript object to java objects