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).
Related
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.
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.
This is my HTML form :
<form action="supplierportal_home.jsp">
<select id="contract" name="contract">
<option selected="selected">Please Select</option>
<option value="open" >Open</option>
<option value="limited" >Limited</option>
</select>
<input type="text" name="cpv_code" placeholder="<%= cpvOrTenderNo %>">
<button type="submit">FIND <%= contractOrTender %></button>
</form>
And i am getting in JSP
String contract=request.getParameter("contract");
System.out.println("%%%"+contract);
String cpv_code=request.getParameter("cpv_code");
System.out.println("%%%"+cpv_code);
Here is the problem.Seems very nasty.
When I enter both the values then only parameters getting in jsp If I select only the contract from options then null is coming if i give cpv-code then the parameter is coming ...can any one please help to get out this ...
Why my select option values are depending on the other form element cpv-code value,please show some cause for this.
Thanks.
<button type="submit">FIND <%= contractOrTender %></button>
In this example whenever you will enter text in textbox then only contractOrTender
this value can be objtain from textbox.
But as you not entering any value in textbox and trying to access value from dropdown it will come 'null' only. as boz at this time <%= contractOrTender %> this value is null.
which an error in jsp page. thats why you not getting dropdown value even if you dont enter any value in textbox.
Solution
Try to set some default value to textbox. or
change your HTML code.
I have just tested the code and it seems to work fine. No value is depending on the other. Only suggestion is add method="POST" if you don't want the data to be sent over URL.
index.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Index Page</title>
</head>
<body>
<form action="upload.jsp" method="POST">
<select id="contract" name="contract">
<option selected="selected">Please Select</option>
<option value="open" >Open</option>
<option value="limited" >Limited</option>
</select>
<input type="text" name="cpv_code" />
<button type="submit">FIND</button>
</form>
</body>
</html>
upload.jsp:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Upload JSP</title>
</head>
<body>
<%
String contract=request.getParameter("contract");
System.out.println("%%%"+contract);
String cpv_code=request.getParameter("cpv_code");
System.out.println("%%%"+cpv_code);
%>
</body>
</html>
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
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");
}
%>