unable to get the passed data from servlet [duplicate] - java

This question already has answers here:
How should I use servlets and Ajax?
(7 answers)
Closed 7 years ago.
I'm writing a program that is working fine with the regular jsp-servlet interaction. But when I submit the same with ajasx, it is not working fine. Below is my code.
JSP-Servlet (w/o Ajax)
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form name="f1" id="f1" method="post" action="Controller">
<input type="text" name="name1" id="name1" /> <input type="submit" />
</form>
</body>
</html>
Controller
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nsme = request.getParameter("name1");
request.setAttribute("name", nsme);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
index1.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" value="${name}" />
</body>
</html>
With Ajax
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
</head>
<body>
<form name="f1" id="f1">
<input type="text" name="name1" id="name1" /> <input type="submit"
name="x" id="x" />
</form>
<script type="text/javascript" src="SampleJS.js"></script>
</body>
</html>
SampleJS.js
$('#x').click(function() {
$.ajax({
type : 'POST',
url : 'Controller',
success : function(data) {
console.log(data)
},
});
});
Controller
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nsme = request.getParameter("name1");
request.setAttribute("name", nsme);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
index1.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="text" value="${name}" />
</body>
</html>
Here when I use the first method, The name is printed correctly. When I use it with AJAX, to my surprise nothing gets printed in the textbox.
Basically, We use request.getRequestDispatcher("GetCaseData.jsp").forward(request, response); to go to another page. What would be the alternative of it in my case?
please let me know where am I going wrong and how can I fix this.

In the Ajax case you don't send any form data. Use $.serialize to serialize the form data and send it to the server:
$('#x').click(function() {
var formData = $("#f1").serialize();
$.ajax({
type : 'POST',
url : 'Controller',
data : formData,
success : function(data) {
console.log(data)
},
});
});

After adding formData in your js file
Check your browser console
You can find your whole index1.jsp html content printed in console
with ${name} replaced with name you entered.
AJAX calls are advised to be used to stay in same page and update page content without page refresh after call.
In above code with AJAX, you are in index.jsp only even after AJAX call completion.
You can switch to other page (index1.jsp in your case) after AJAX call by using javascript window.location.href=<your file name >. However in your case, the page switches to index1.jsp but as new request. You can observe the same in your URL bar of browser. As you are storing the name attibute in request scope. You wont see the value you entered in name field of index1.jsp.

Related

How to use user entered value in all the jsp pages in application

In this scenario im having a login.jsp and 4 other jsps like file1.jsp,file2.jsp,file3.jsp and file4.jsp.
login.jsp Code :
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<form action="application" method="post">
UserName : <input type="text" name="uname" > <br><br>
password : <input type="password"> <br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
When i Submit the form it goes to ApplicationServlet.java and the code is :
#WebServlet("/application")
public class ApplicationServlet extends HttpServlet {
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username = req.getParameter("uname");
System.out.println("Entered username is : " + username);
req.setAttribute("user", username);
RequestDispatcher rd = req.getRequestDispatcher("File1.jsp");
rd.forward(req, resp);
} }
then i forwarded the request to file1.jsp here is the code.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name = (String)request.getAttribute("user"); %>
Welcome <%=name %>
</body>
</html>
Here in file1.jsp im able to display the user entered value.
but when im using the same thing in file2.jsp im getting null.
file2.jsp code is
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% String name = (String)request.getAttribute("user"); %>
Welcome <%=name %>
</body>
</html>
How can i reuse the user entered value in other jsp files.
In your case you can use HttpSession for setting value in you servlet after successful login.
Eg:
HttpSession session = request.getSession();
session.setAttribute("user", name);
There are 4 scope of a variable in JSP.
page
request
session
application.
use session scope if you want your variables in all pages for limited period of time.
use application scope if you want to retain values of your variable all the time.
for more understanding, read these 4 implicit objects in JSP. Total 9 implicit objects are there.

Page not found error for html file used in jsp project

I am learning Jsp and working on a basic Jsp project which is using a HTML file in it.
When I run the HTML file, it gives page not found error and while running the Jsp, it does not load the form and button in it.
Kindly help me out.
Below are my HTML and Jsp files:
Index.html
<html>
<head><title>New</title></head>
<body>
<form action="myFirstJsp.jsp">
<input type="text" name="uname">
<input type="submit" name="submit">
</form>
</body>
</html>
myFirstJsp.jsp
<%#page import="java.util.Calendar"%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>My First JSP</title>
</head>
<body>
<%String name= request.getParameter("uname");
out.println("Welcome " +name);%>
</body>
</html>
First check if your server is up & your app is well deployed on it. Check the request url being fired from the browser.
Also
It's a good practice to use jstl tag to provide urls in your application. It computes the context path averting 404 page not found errors due to incorrect urls.
<form action="<c:url value="/myFirstJsp.jsp">
Add the jstl library in your jsp
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"/>

Google Search Api Couldn't get Result More Than 4?

Here is my code that Search Result From Google.somewhere is written that i have to set 'start' to '0' but as i'm a very Newbee in java i really don't khow what should i do.so any help will appreciate.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%#page import="com.demo.GoogleSearch"%>
<%#page import="java.util.List"%><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%#include file="SearchGoogle.jsp" %>
<%
String word=request.getParameter("searchWord");
List<String>urls=GoogleSearch.searchWord(word);
%>
<table>
<%
int b = urls.size();
for(int i=0;i<b;i++){
//if (i < b)
%>
<tr><td><%=urls.get(i) %></td></tr>
<%} %>
</table>
</body>
</html>
And Here is the page for Sesrch:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="SearchResults.jsp">
<table>
<tr><td>
Enter Search Word:</td><td>
<input type="text" name="searchWord"/></td></tr>
<tr><td colspan="2">
<input type="submit" value="search">
</td></tr>
</table>
</form>
</body>
</html>
I think 4 search results is Google's limit on results when calling it through their API, unless you want to pay for the searches. In uni I had to make a project which needed to get results from a search engine, we used GigaBlast since it has a nice API and a much larger limit of results returned per search (50 if I recall right).

get value from textfield html into java variable

I am tring to run function that get 2 variables one of them should get the value from textfield.
any advice?
<%#page import="root.SQLQuery"%>
<%# page language="java" contentType="text/html; charset=windows-1255"
pageEncoding="windows-1255"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%# page import="root.SQLQuery"%>
<%
String emp_email = session.getAttribute("email").toString();
SQLQuery sql = new SQLQuery();
String link="";
%>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<title>Insert title here</title>
</head>
<body>
<center>
<table width="900" height="900">
<iframe src="http://www.aol.com/av" width="900" height="900"></iframe>
<center>
<font color=red>your email is:<%=emp_email%>></font>
Copy the link here: <input type="text" name="videoLink" id="link">
<input type="submit" value="submit" onclick="<%sql.meetingUpdateVideoLink(emp_email, request.getParameter("videoLink").toString()); %>>">
</center>
</table>
</center>
I am tring to insert the fanction meetingUpdateVideoLink() two variables:
email - that easy I get from session.
Link - that is the tricky one I need to get it from the textfield name="videoLink"
Thanks,
Cfir

Redirecting users in JSP from within a includes - java syntax error

So my setup for my web application is that I have a general header and footer and then I just include them in all my other pages so that all the common elements for all the pages are in a single page. The only problem I'm running into is when I want to redirect the user back to a login page if the "username" session has not already been created.
The problem that I"m running into is that I have an if statement at the top of my header.jsp and then I have the else in the footer.jsp and it is giving me a java syntax error. Any ideas? Here is the code I'm referring to...
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%
if(session.getAttribute("username") != null)
{
%>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<!-- CSS files -->
<link rel="stylesheet" href="../CSS/headerStyle.css" type="text/css" media="screen" />
<title>Insert title here</title>
</head>
<body>
<div id="container">
<div id="header">
<div id="headerTitle">Title</div>
</div>
</div>
<%} %>
And then here is the footer
<!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">
<link rel="stylesheet" href="../CSS/headerStyle.css" type="text/css" media="screen" />
</head>
<body>
<div id="footer"></div>
</body>
</html>
<%
else
{
response.sendRedirect("../wa_login/login.jsp");
}
%>
However it is giving me an error on the else statement because the else doesn't have an if statement because it's in the header file.
Each .jsp has to compile as an independent unit. Hence the error.
Why not just perform the check in the header and redirect from there ?
<%
if(session.getAttribute("username") == null)
{
response.sendRedirect("../wa_login/login.jsp");
}
%>

Categories