How to display List results in JSP Page? - java

I Have a class that find Google search results. And i have a JSP page that i want to show the results in. But i can't do it.
Heres my UrlOku class :
public static void GetUrl() {
final String keyword = "emre varol";
final String url = "https://www.google.com/search?q="+keyword;
try {
final Document document = Jsoup.connect(url).get();
List<String> myList = new ArrayList<String>();
for(Element row: document.select("div[class=g]")) {
final String title = row.select("div[class=TbwUpd NJjxre]").text();
myList.add(title);
}
}
And heres my JSP page :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import="urlpaket.Urloku" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="menu.jsp"></jsp:include>
<%
if(session.getAttribute("username")==null){
response.sendRedirect("login.jsp");
}
%>
WELCOME ${username}
<%
Urloku oku = new Urloku();
oku.GetUrl();
%>
<c:forEach items="${myList}" var="item">
<tr>
<td><c:out value="${item.title}" /></td>
</tr>
</br>
</c:forEach>
And there is nothing in the welcome.jsp file.
Any help would be appreciated!

How is myList variable available in you JSP? This needs to be set in pageContext before accessing it using ${myList}.
You can change the signature to public static List GetUrl()
and do something like this
Urloku oku = new Urloku();
List<String> myList = oku.GetUrl();
pageContext.setAttribute("myList ",myList );
Now it should be available.

Related

Java-Jsp The server encountered an unexpected condition that prevented it from fulfilling the request

My error above , I did write.
Now , I have 4 my file.The problem is simple.(denetleme.jsp,BasvuruFormu.jsp,Elemanlar.java) and I want to after in BasvuruFormu.jsp to submit clicked it is going to denetleme.jsp then there applied kontrolMethod after if boolean hata = false I want to OnayYok.jsp but i cant go.
BasvuruFormu.jsp also goes after clicking on the button denetleme.jsp then Elemanlar.java check in KontrolMethod() but boolean hata = false but after freezing false, OnayYok.jsp does not go
1)denetleme.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%# page import="java.util.*" %>
<jsp:useBean id="kahveCekirdegi" class="com.jsp.Elemanlar" scope="request">
<jsp:setProperty property="ad" name="kahveCekirdegi" />
<jsp:setProperty property="yas" name="kahveCekirdegi" />
</jsp:useBean>
<%
if(kahveCekirdegi.kontrolMethod()){
%>
<jsp:forward page="onay.jsp"/>
<%
} else {
%>
<jsp:forward page="OnayYok.jsp"/>
<%
}
%>
</body>
</html>
2)BasvuruFormu.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "denetleme.jsp" method = "post">
<table border="1">
<tr>
<td>Ad
<input type = "text" name = "ad"></td>
</tr>
<tr>
<td>Yas
<input type = "text" name = "yas"></td>
</tr>
</table>
<input type = "submit" value = "submit" style="background-color: red; color : black;">
</form>
</body>
</html>
3)Elemanlar.java
package com.jsp;
import java.util.Hashtable;
public class Elemanlar {
private String ad;
private String yas;
private Hashtable<String, String> hatalar;
public void setYas(String yas) {
this.yas = yas;
}
public String getYas() {
return this.yas;
}
public void setAd(String ad) {
this.ad = ad;
}
public String getAd() {
return this.ad;
}
public boolean kontrolMethod() {
boolean hata = true;
if (ad.equals("")) {
hatalar.put("ad", "lütfen ad giriniz");
ad = "";
hata = false;
}
if (yas.equals("")) {
hatalar.put("yas", "Lütfen geçerli bir yaş giriniz");
yas = "";
hata = false;
}
return hata;
}
public String getHataMesajlari(String hataCumleleri) {
String hataMesajlari = (String) hatalar.get(hataCumleleri.trim());
return (hataMesajlari == null) ? "" : hataMesajlari;
}
public void setHatalar(String formElemani, String verilenHataMesaji) {
hatalar.put(formElemani, verilenHataMesaji);
}
}
4)OnayYok.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>Onay yok jsp</h3>
</body>
</html>
Example Ad and Yas is empty photography below.
then I clicked submit
Misateke :
https://i.stack.imgur.com/ay6T7.png
https://i.stack.imgur.com/udYKs.png

Jsp include another jsp with Struts2, but no result

I used a jsp include another jsp, but I don't get any result, only html tag content.
HelloEmp.jsp: it had iterator value with stuts2 tag.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix = "s" uri = "/struts-tags" %>
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Example of List</h2>
<s:iterator value="helloList">
<s:property /><br/>
</s:iterator>
</body>
</html>
HelloAction.java: it had a string array
import java.util.ArrayList;
import java.util.List;
public class HelloAction {
private List<String> helloList = new ArrayList<String>();
public String execute() throws Exception {
helloList.add("Jacky");
helloList.add("Natali");
return "success";
}
public List<String> getHelloList() {
return helloList;
}
public void setHelloList(List<String> helloList) {
this.helloList = helloList;
}
}
employees.jsp: it included HelloEmp.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<title>Employees</title>
</head>
<body>
<p>An example of the include tag: </p>
<s:include value = "example/HelloEmp.jsp"/>
</body>
</html>
struts.xml:
<package name = "helloworld" extends = "struts-default">
<action name = "hello" class = "example.HelloAction" method = "execute">
<result name = "success">HelloEmp.jsp</result>
</action>
</package>
When I opened employees.jsp, only can see tag content in HelloEmp.jsp, I can't see the iterator value:
But if I directly open HelloEmp.jsp, I got the result:
Please help me how to fix it? thank you!
Instead of using value use page
<jsp:include page="Demo.jsp" /> might work.
Finally I use action tag instead,
<s:action name="action_name" executeResult="true"/>
that works for me!
Thank you for you guys.

spring:mvcUrl mapping wrong id

I am trying to map productId to URL in my jsp page, but the value is wrong.
The URL is returning Título 1 but the response is returning the right value:
HTML Mapping
My jsp code:
<%# page language="java" contentType="text/html; charset=iso-8859-1" pageEncoding="iso-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%# taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Insert title here</title>
</head>
<body>
<div>
${success}
</div>
<table>
<tr>
<td>Id</td>
<td>Titulo</td>
<td>Valores</td>
</tr>
<c:forEach items="${products}" var="product">
<tr>
<td>${product.id}</td>
<td>${product.title}</td>
<td>
<c:forEach items="${product.prices}" var="price">
[${price.value} - ${price.bookType}]
</c:forEach>
</td>
</tr>
</c:forEach>
</table>
My Java Controller Code:
#RequestMapping(method=RequestMethod.GET)
public ModelAndView list() {
ModelAndView modelAndView = new ModelAndView("products/list");
modelAndView.addObject("products", productDAO.list());
return modelAndView;
}
My Java DAO Code:
public List<Product> list() {
return manager.createQuery("select distinct(p) from Product p join fetch p.prices", Product.class).getResultList();
}
Unfortunately I cannot see your method 'show'.
I guess you have a misprint in the path:
#RequestMapping("/show{/id}")
//--------------------^^ WRONG
public String show(#PathVariable("id") long id) {
...
}
instead
#RequestMapping("/show/{id}")
//--------------------^^ RIGHT

Model Data is not getting transferred to the jsp page

I have designed a Login page using Spring MVC,JPA(Hibernate) and Jsp.
Please find the login.jsp :-
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<h2>Login page</h2>
<form:form method="POST" modelAttribute="loginBean" action="showProfile">
<table>
<tr>
<td>UserName :<form:input path="userName"/></td>
</tr>
<tr>
<td>Organization Id :<form:input path="OrgId" /></td>
</tr>
<tr>
<td>Password : <form:input path="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td>
</tr>
</table>
</form:form>
</body>
</html>
profilePage.jsp:-
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<table border="2" width="70%" cellpadding="2">
<tr>
<th>UserName</th>
<th>Password</th>
</tr>
<c:forEach var="staff" items="${staffData}">
<tr>
<td>${staff.userName}</td>
<td>${staff.password}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
LoginController.java:-
#Controller
public class LoginController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String showLoginPage(Model model) {
model.addAttribute("loginBean", new LoginBean());
return "login";
}
#RequestMapping(value = "/showProfile", method = RequestMethod.POST)
public ModelAndView redirectToprofile(#ModelAttribute("loginBean") LoginBean loginBean) {
StaffServiceImpl staffServiceImpl = new StaffServiceImpl();
Staff staff = staffServiceImpl.authenticateStaff(loginBean);
if (null != staff) {
return new ModelAndView("redirect:/profilePage","staffData",staff);
}
return new ModelAndView("profileNotFound");
}
#RequestMapping(value = "/profilePage", method = RequestMethod.GET)
public String showProfilePage() {
return "profilePage";
}
After giving the valid details in the Login.jsp page it is redirecting to profilePage.jsp but the data in profilePage.jsp is not correct. Please help me to understand where i am doing the mistake.
The profilePage.jsp is displayed a below:-
UserName Password
${staff.userName} ${staff.password}
The Value of this variable is not getting displayed.
profilePage.jsp : Add isELIgnored="false" in page directive.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
Update your controller with the following :
#Controller
public class LoginController {
#RequestMapping(value = "/", method = RequestMethod.GET)
public String showLoginPage(Model model) {
model.addAttribute("loginBean", new LoginBean());
return "login";
}
#RequestMapping(value = "/showProfile", method = RequestMethod.POST)
public String redirectToprofile(#ModelAttribute("loginBean") LoginBean loginBean , Model model) {
StaffServiceImpl staffServiceImpl = new StaffServiceImpl();
Staff staff = staffServiceImpl.authenticateStaff(loginBean);
if (null != staff) {
model.addAttribute("staffData",staff);
return "profilePage";
}
return "profileNotFound";
}
the above answer is correct, and this is an explination:
The isELIgnored attribute gives you the ability to disable the evaluation of Expression Language (EL) expressions which has been introduced in JSP 2.0.
The default value of the attribute is true, meaning that expressions, ${...}, are evaluated as dictated by the JSP specification. If the attribute is set to false, then expressions are not evaluated but rather treated as static text.
Source :enter link description here

How to pass the array list from servlet to JSP?

In the servlet:
List<myItem> yourObjectToReturn = search.parserContent();
request.setAttribute("yourObjectToReturn",yourObjectToReturn);
the array yourObjectToReturn consists 3 variable(id, txtfile, sentence) which you can see from
the myItem class
public class myItem{
String sentence;
int id;
String txtfile;
// public myItem(){
// }
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
public String getTxtfile(){
return txtfile;
}
public void setTxtfile(String txtfile){
this.txtfile = txtfile;
}
public String getSentence(){
return sentence;
}
public void setSentence(String sentence){
this.sentence = sentence;
}
}
how to display the id, txtfile, sentence in the JSP separately? How to pass the arraylist from servlet to JSP .
the JSP : how to edit my JSP. I got error of my JSP:
type safety: unchecked cast from objectto arraylist
<%# page import="java.io.*" %>
<%# page import="java.net.*" %>
<%# page import="java.util.*" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# 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>
<% List<myItem> myList = (ArrayList<myItem>) request.getAttribute("yourObjectToReturn"); %>
The search Result SENTENCE IS: <%=myList %> --%>
</body>
</html>
Don't use scriptlets in your jsp page.
Include the JSTL standard taglib via:
<%# taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
Then in your JSP use the iteration tag:
<c:forEach items="${requestScope.yourObjectToReturn}" var="current">
<c:if test="${current.sentence== 'secret' }">
<h1>seeeeeeeeeecret revealed</h1>
</c:if>
</c:forEach>
Where:
${requestScope.yourObjectToReturn} is your collection object.
And (during each iteration):
${current} is your actual element.
For further reference look http://docs.oracle.com/javaee/5/tutorial/doc/bnahq.html
And to avoid weird errors: don't forget to import myItem class (Should really be MyItem, thou...)
EDIT: Before digging deep into JSTL, I suggest you to take a reading of this other question. Particularly focus on the selected answer, it provides great insights.
To retrieve the Id and Txtfile from the List you need to iterate with using for example a for loop like:
...
for (int i=0; i < myList.size(); i++) {
%>
<%=myList.get(i).getId()%>
<%=myList.get(i).getTxtfile())%>
<%}%>
You can use for loop and html together as follows:
<%
#SupressWarnings("unchecked")
List<mtItem> myList
for (MyItem myitem : myList)
{
%>
The search result is <%=myitem%>
<%
}
%>

Categories

Resources