I have a question, how to call a servlet from a jsp(chart.jsp) without using <jsp:include page="/servletURL" />because I tried before and I don't know if this is the right reason but it crashes when I use this code above.
I put in my doGet() method to retrieve information from DB and populate my dropdownlist (in chart.jsp) using JSTL+option and then redirect to my page (the same page), what I believe is that everytime the browser writes a new page using c:forEachtag it calls again my servlet and there is a never ending loop (Again, that's just my presumptions)
Here is my code to make it more clear:
my servlet:
ArrayList<Machine> foundMachines = MachineDB.getAllMachines();
request.getSession().setAttribute("foundMachineList", foundMachines);
RequestDispatcher rd = request.getRequestDispatcher("charts/chart.jsp");
rd.forward(request, response);
my jsp:
<jsp:include page="/searchServlet" />
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="machine" items="${sessionScope.foundMachineList}">
<option value="${machine.machineId}">${Machine.machineName}
</option>
</c:forEach>
So my question is why my <jsp:include page="/servletURL" /> tag crashes my page and how to fix it. Any sugestion is welcome
Use
response.sendRedirect("//your servlet name");
Most probably is not related but change
${Machine.machineName} to ${machine.machineName}
Related
I am writing a JSP code which goes like this:
<% if(---)
{----}
else
{ ---
%>
<jsp:forward page="error.jsp">
<jsp:param name="" value=""/>
</jsp:forward>
<%
}
%>
The error.jsp is in the same directory as the current jsp file still it is throwing "class not found exception". What to do?
Not much clear from your question what you are trying to achieve but If you are adding the another jsp page within current jsp page then use jsp:include otherwise if trying to redirect to another jsp page then following could be used.
Use in scriptlet response.sendRedirect in your case as below:
<%
response.setHeader("header_key", "header_value");
response.sendRedirect("your_page_location")
%>
Above could be used for external pages as well like "www.google.com"
On the other hand you could use request forward with all old parameters and data from calling page. See below:
RequestDispatcher rd = servletContext.getRequestDispatcher("/pathToResource");
rd.forward(request, response);
Also, another easy way could be to submit a form with action="your_jsp.jsp" but this should be used in case of form submissions where you are sending some data etc.
Make choice for any of above options on a case basis.
Hope it helps!
I want to open a jsp page without accessing my servlete code. i.e. I neither have to input my url in (action="url") my jsp code nor have to access my Servlete code.
<form id="main" method="post" name="main" action="dpRegPost" onsubmit="return validate();">
Can anyone help me in this?
You can add javascript to your jsp file
<script type="text/javascript">
window.location.href = "www.google.com";
</script>
or using jsp
<%
response.sendRedirect("www.google.com");
%>
You can also try this
<jsp:forward page = "abc.jsp" />
Use jstl taglibrary in your current jsp page.Make available the taglibrary using below code
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Use Following code in jsp to redirect
<c:redirect url="/xxx.jsp"/>
Try this:
<form id="main" method="post" name="main" action="" onsubmit="redirect(this);">
<input type="submit" name="submit"/>
</form>
function redirect(elem){
elem.setAttribute("action","somepage.jsp");
elem.submit();
}
You can also call page directly with:
<jsp:redirect page="xyz.jsp" />
use the following code to redirect on another page in js...
$('#abc_id').click(function() {
updateSubContent("abc.jsp");
});
I am working on a Java application eInvoice application. The purpose of app is to add eInvoices and then display the eInvoices I have added (Works totally fine upto here). However when I display invoices I want the company name to be hyperlinked, and after clicking on the link that particular invoice details should be displayed in a Jsp page. Please note I am passing information to servlet and then I am storing the info in java class using getters and Setters. Multiple records are stored in the Arraylist object. I cannot seem to find the way to do this. Any help is greatly appreciated.
<%
float bigTotal=0;
try
{
#SuppressWarnings("unchecked")
ArrayList<UserInvoice> myVal = (ArrayList<UserInvoice>)session.getAttribute("eInvoice");
out.write("<table border='0'>");
out.write("<th width='200'>Invoice No.</th><th width='300'>Client Name</th> <th width='200'>Total</th><th width='200'>Payment Due Date</th>");
for(UserInvoice invoices:myVal)
{
out.write("<tr>");
out.write("<td height='25'>");
out.write(""+invoices.getInvoiceNo());
out.write("</td>");
out.write("<td>");
out.write(invoices.getClientName());
out.write("</td>");
out.write("<td>");
out.write(String.valueOf(invoices.getTotal()));
out.write("</td>");
out.write("<td>");
out.write(invoices.getPayDueDate());
out.write("</td>");
out.write("</tr>");
bigTotal = bigTotal+invoices.getTotal();
}
out.write("</table>");
}
catch(Exception ex)
{
out.write("<h1>Looks like you haven't added any invoices yet.</h1>");
}
You can use JSTL foreach:
<%# page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<% request.setAttribute("eInvoices", eInvoices); %>
<table>
<c:forEach items="${eInvoices}" var="value">
<tr><td><c:out value="${value.name}"/></td></tr>
</c:forEach>
</table>
Use AJAX to get the details.
(when you click on the invoice name, an HTTP request is made to the page to get the details. These details are returned as HTML with a content-type of 'text/html')
Examples:
loading detail record through ajax
Shopify API call using AJAX and JSP
AJAX with JQuery
I have the working Java code here to display a user's username upon login:
<%= "Welcome " + session.getAttribute("username")%>
If a user is not logged in and at my index.jsp page, it will display "Welcome null".
However, I want to display "Welcome Guest" instead.
Is there any way to do this?
You can do that with JSTL:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Welcome, <c:out value="${sessionScope.username}" default="guest" />
Use JSTL tag libraries. JSP scriplets are deprecated almost a decade back.
This is how you do it using JSTL:
Welcome, <c:out value = "${sessionScope.username == null ? 'Guest' : sessionScope.username}" />
See also:
Our JSTL tag Wiki.
How to avoid Java code in JSP files?
I am trying (and learning) to build a java web framework, and in the process of developing its' code generator based on the content of the database. In the view making process, I stumble in a difficulty, which I don't know how to solve it.
Firstly, I want all the pages to be created using the following index.jsp :
<body>
<%# include file="header.jsp" %>
<hr/>
<%# include file="body.jsp" %>
<hr/>
<%# include file="footer.jsp" %>
</body>
And, in the body.jsp, I want it to be like this :
<jsp:include page="${application_modul}" %>
Where application_modul is an attribute defined in its' controller this way :
request.setAttribute("application_modul","user_account\\view_user_account.jsp");
It can find the file correctly, but the processed jsp is not what I expected. Here :
<c:forEach items="[application.models.UserAccountModel#18a49e0, application.models.UserAccountModel#1f82982]" var="item" varStatus="status" >
<tr>
....
You can see the items attribute of jstl forEach, got its variable name (toString())...
Any Idea what the problem is????
I hope I describe my problem correctly
Many thanks!
PS :
I already create a quick fix for this, but not what I want it though. In the generated view_user_account.jsp, I do it like this :
<body>
<%# include file="header.jsp" %>
<hr/>
<c:forEach items="${row}" var="item" varStatus="status" >
<tr>
....
<hr/>
<%# include file="footer.jsp" %>
</body>
You can see that I create the whole file here...
EDITED:
PS : ${row} is an ArrayList populated with data from certain table
So, to summarize your problem in a single sentence, JSTL tags are not been parsed and they end up plain in generated HTML output?
You need to declare JSTL taglib in top of the JSP page where you're using JSTL tags to get them to run. For the JSTL core taglib, that'll be
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
I am not sure but, Try this...
index.jsp
<jsp:param name="parameterName" value="{parameterValue | <%= expression %>}" />