Open mutiple pdf files in separate tabs - java

Hi I am trying to open multiple pdf files on a click , I am trying in this way.
Considering user will select multiple options to get pdf files by selecting multiple radio buttons.
<html>
<head>
<title>sendRedirect Program using Servlet</title>
</head>
<body>
<form name="pdf" target="_blank" method="get" action="http://localhost:8080/SendRedirectProg/sr">
<table border="1" width="500">
<tr>
<td>Select PDF:</td>
<td><INPUT type="radio" name="pdf" size="20" value="PDF 1">PDF 1
<INPUT type="radio" name="pdf" size="20" value="PDF 2">PDF 2
<INPUT type="radio" name="pdf" size="20" value="PDF 3">PDF 3</td>
</tr>
<tr>
<td><INPUT type="submit" size="20"></td>
</tr>
</form>
</table>
</body> </html>
So consider uer will select all 3 radio buttons and I want to open all these pdf files in different tabs.
I am trying in this way.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SendRedirectProg extends HttpServlet
{
public void method(ActionRequest req, ActionResponse res) throws
{
PrintWriter p=res.getWriter();
res.setContentType("text/plain");
String s=req.getParameter("pdf");
if(s.equals("PDF 1") && s.equals("PDF 2") && s.equals("PDF 3"))
{
res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF1.pdf");
res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF2.pdf");
res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoMCA 3.pdf");
}
}
but it only displays last one, I am understanding use of res.sendRedirect, but still how we can achieve this ?
Any suggestions?

You cannot open 3 tabs in one redirect. What you can do is use jQuery or javascript
Redirect to a page where you write the url's to 3 different hidden div's or generate the script directly
onload of the page, use javascript to open them.
window.open(url1,'_blank');
window.open(url2,'_blank');
window.open(url3,'_blank');

target="_blank" is your answer ;)
<form name="pdf" method="get" target="_blank" action="http://localhost:8080/SendRedirectProg/sr">
More information: http://www.w3schools.com/tags/att_form_target.asp
There is also problem in your java code, you should use only one redirect, accordingly to passed value.
if(s.equals("PDF 1"))
{
res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF1.pdf");
}
if(s.equals("PDF 2"))
{
res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoPDF2.pdf");
}
if(s.equals("PDF 3"))
{
res.sendRedirect("http://localhost:8080/SendRedirectProg/DemoMCA 3.pdf");
}

Related

How to properly include a jsp in another jsp

I'm making a form to add events to a list but I need to be able to see in that form the previous events from that list. I have a jsp that shows a list of events but it takes an attribute usually added by the controller when you access the list directly from the browser.
So how can I add the list jsp filling that attribute so it shows a list and not just the headers?
I have alreay included the jsp using
<jsp:include page="comp_list.jsp"></jsp:include>
And it shows the headers but as there is no attribute it shows no list. I tried adding attributes to the include like:
<jsp:include page="comp_list.jsp">
<jsp:attribute name="compensaciones">
${compensaciones}
</jsp:attribute>
</jsp:include>
But when I do it it shows an error telling me that this cannot be done.
Also tried params but that would not be the answer for me because params are treated in the controller and not in the jsp itself.
I'm just getting the header of the list which is fine, but i would like to see the list.
Edit: this is how i build the list in case you are wondering
<tbody>
<c:forEach var="comp" items="${compensaciones}">
<td>${comp.getSomething()}</td>
...
</c:forEach>
</tbody>
<jsp:include page="pagename.jsp" />
i thing you are not provide extension in your include tag
Sometimes since I've had prior experience back in the days with HTML and PHP, we simply just would include things like navigation and header for easier management.
I'm unsure for what purpose you're including JSP, but here's an example of how I've done it since I include JSP if a boolean is true.
The site that is accessed:
<div class="row">
<div class="col-lg-2">
</div>
<div class="col-lg-8">
<%
if (loggedin) {
%>
<%# include file="includes/profile.jsp" %>
<% } else {
out.println("<div><h2>You aren't logged in!</h2></div>");
}
%>
</div>
<div class="col-lg-2">
</div>
</div>
And the top and bottom of the JSP file I'm including doesn't contain things like head, title, html, body etc.
<%
User currUser = null;
currUser = (User) session.getAttribute("user");
%>
<div>
<h2>Du er logget ind, velkommen <% out.println(currUser.getUsername().toUpperCase()); %></h2> <br>
<h5>Din Saldo: <b><% if (currUser.getBalance() < 0) { out.println("<font color='red'>" + currUser.getBalance() + "</font>");} else { out.println("<font color='green'>" + currUser.getBalance() + "</font>");} %></b></h5>
<br>
<form class="form" method="post" action="#">
<h4>Oplysninger: </h4>
<h6>
For at ændre oplysninger skal du indtaste dit kodeord!
</h6>
Nuværende Kode: <input type="password" class="form-control" placeholder="kodeord" name="password" required>
<hr>
Email: <input type="text" class="form-control" value="<% out.println(currUser.getEmail()); %>" name="email"> <br>
Brugernavn: <input type="text" class="form-control" value="<% out.println(currUser.getUsername()); %>" name="username"> <br>
<input type="submit" class="btn btn-default" value="Indsend Oplysninger" />
</form>
</div>
When importing JSP into JSP, it's important to know that they will conflict if 1. Duplicate Local Variables. 2. Certain HTML tags aren't correct.
Your intended use seems very complicated to me since I'm not quite understanding it, sorry :3 but if you can't get it working consider to throw it to a session and if it's a one time use then remove it once you used it.
This is working from dacades
<%# include file="../scripts/standard_head_declaration.jsp" %>

Multiple submission in a page using Spring MVC

I am implementing a Web-based Java app using Spring MVC.
What I am doing now is to provide a page which allow users to select from a set of radio buttons and export a set of data in CSV by clicking the "Submit" button.
However, I have encountered a situation.
After users download one CSV, whenever they want to download another one, if they click "Submit" button again, the download process will not be fired and a dialog saying "Action already submitted. Wait until the action is complete." will show up.
Here is my codes:
<form:form action="handle_export?user_id=${userId}" method="post">
<div>
<table>
<tr>
<td>
<input id="set0" type="radio" name="exportType" value="0"/>
</td>
<td>
<label for="set0">dataset0 Export</label>
</td>
</tr>
<tr>
<td>
<input id="set1" type="radio" name="exportType" value="1"/>
</td>
<td>
<label for="set1">dataset1 Export</label>
</td>
</tr>
</table>
</div>
<input type="button" class="submit button-1" value="Download" name="bottom_Download" />
</form:form>
Any chance that I can fire the "Submit" button multiple times?
Thanks a lot.
I've been struggling with blackboard and I have solved doing:
-Remove "action" from FORM
-Create a function on JS, by example: generateCSV()
-Function would be like that:
var userid = document.getElementById('userid');
window.open('handle_export?user_id=' + userid);
On handle_export, retrieve the values following:
String userid = request.getParameter("userid");
// ...
// generate and download file
This way, its opened in a new window and blackboard not show up the dialog saying "Action already submitted. Wait until the action is complete." .
I hope this helps, regards.

checking which check boxes are selected using JAVA ( a jsp)

I am trying to create a servlet that displays a simple form with checkboxes , when the user selects the number of checkboxes he wants and clicks on a "confirm" the POST request in my servlet checks for which boxes have been checked and queries the database based .
I am unsure on how to do this in Java as the user may select either 1 or more checkboxes . if somebody could explain this with a small example this would be great.
I am very new to programming and would provide a code snippet if I knew how to do it .
<%# page language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Multiple Checkbox</title>
</head>
<body>
<form name="form1" onsubmit="checkBoxValidation()">
<h3>Select your favorite Fruits</h3>
<p><input type="checkbox" name="fruit" value="Mango"/>Mango</p>
<p><input type="checkbox" name="fruit" value="Apple"/>Apple</p>
<p><input type="checkbox" name="fruit" value="Grapes"/>Grapes</p>
<p><input type="checkbox" name="fruit" value="Papaya"/>Papaya</p>
<p><input type="checkbox" name="fruit" value="Lychee"/>Lychee</p>
<p><input type="checkbox" name="fruit" value="Pineapple"/>Pineapple</p>
<p><input type="submit" value="submit"/>
</form>
<%String fruits[]= request.getParameterValues("fruit");
if(fruits != null){%>
<h4>I likes fruit/s mostly</h4>
<ul><%for(int i=0; i<fruits.length; i++){%>
<li><%=fruits[i]%></li><%}%>
</ul><%}%>
</body>
</html>
Run this sample jsp on your web container to get some basic idea on how it works. You need to move the display logic on this page that gets request parameter into your servlet code on form submission. This example can be found from here. Hope this would help.
This is actually the HTML form behavior question. When you check a few checkboxes with one "name" attribute and different "value" attributes and press submit button, your browser will send request to the server with checked checkbox values. So you can get value names from this url parameters.
For example:
<form name="input" action="html_form_action.asp" method="get">
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
<br><br>
<input type="submit" value="Submit">
</form>
If you check both of checkboxes your server will receive this parameters like so:
http://example.com/your_page.jsp?vehicle=Bike&vehicle=Car
After that you can get values like this:
String checkboxValues = request.getParameter("vehicle");
checkboxValues gets all values separated by comma.
In your servlet you would use getParameter() like so:
request.getParameter( "id_of_checkbox" )
That function returns null if the the box is not checked. So you could do something like:
boolean myCheckBox = request.getParameter( "id_of_checkbox" ) != null;
Now myCheckBox is true if checked, false if not checked.
This one might be neater if you just want the output. Assuming you're using jstl libraries, which I prefer because it makes your pages cleaner:
<c:forEach var='fruitValue' items='${paramValues.fruit}'>
${fruitValue} <br>
</c:forEach>

JavaScript in liferay portlets

Am using Liferay portal 6.1.1 CE.
In my liferay portlets jsp page - view.jsp, I have a form with 2 textfields and 2 radio buttons (user can select only one) and a submit button.
When I click on any of the radio buttons, control goes to a script function which is working properly.
Now I want to pass the values of 2 textfields to the script function. I tried a lot, but no use.
How can I achieve this?
Help me..Thanks in advance.
<script>
function dif()
{
}
</script>
<form name="<portlet:namespace/>fm" method="post" action="<%=updateBookURL.toString()%>">
<table>
<tr>
<th>Leave Application</th>
</tr>
<tr>
<td>Employee <font color=red>*</font></td>
<td>
<input type="text" name="<portlet:namespace/>name"/>
<input type="text" name="<portlet:namespace/>eid" />
</tr>
<td>
Date <font color=red>*</font>
<input type="text" id="<portlet:namespace/>fd" name="<portlet:namespace/>
</td>
<td>
<input type="radio" name="<portlet:namespace/>f" id="f" value="1st half" onClick="f()"/>
First Half&=
<input type="radio" name="<portlet:namespace/>f" id="f" value="2ndhalf" onClick="f()"/>
Second Half
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="save data" size="100px"/></td>
</tr>
</table>
</form>
Getting the value of the text in javascript function is nothing new in liferay it is plain old javascript, following are some links and examples which would help you (the code which I have written would go in your custom function you define within the <script> ... </script> tags):
Get input text value using:
var fdTextValue = document.getElementById("<portlet:namespace/>fd").value;
Get input text value using jQuery (the only problem with this is you have to include jQuery library in your portlet or theme, since starting from Liferay-6 you have Alloy UI included by default):
var fdTextValue = $("#<portlet:namespace/>fd").val();
/* or */
var fdTextValue = $("#<portlet:namespace/>fd").attr('value');
Get input text value using Alloy UI of Liferay:
AUI().use(function(A) {
var fdTextValue = A.one('#<portlet:namespace/>fd').get('value');
/* or */
var fdTextValue = A.one('#<portlet:namespace/>fd').val();
});
Some of the following links might also help you using Alloy UI:
Working with Elements and Events with Alloy UI.
Alloy UI API
It is very simple.
on click event of then in js define this function.
<script>
function callFun()
{
// Here you can use jquery easily
var name = $("#name").val();
alert(name);
}
</script>

servlets connectivity & running on tomcat

How can I use this path: "file:///E:/apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html" ? Is this correct?
This is my html file:
<html>
<form method=post action="../classes/match1">
<body bgcolor="powderblue">
<center><h1>MATCH</h1>
<hr/>
MATCH NO <input type="text" name="op1"/><br/><pre>
DATE <input type="text" name="op2"/><br/><pre>
CITY <input type="text" name="op3"/><br/><pre>
TEAM1 <input type="text" name="op4"/><br/><pre>
TEAM2 <input type="text" name="op5"/><br/><pre>
STADIUM <input type="text" name="op6"/><br/><pre>
WINNER <input type="text" name="op7"/><br/><pre>
MAN OF THE MATCH <input type="text" name="op8"/><br/><pre>
<input type="submit" value="submit"/>&nbsp
<input type="reset" value="reset"/>
</body>
</html>
My servlet code:
import javax.servlet.http.HttpServlet;
import java.io.*;
import java.util.*;
import java.sql.*;
import java.sql.Date.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class match1 extends HttpServlet {
Connection con;
PreparedStatement pst;
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException {
try {
PrintWriter out=res.getWriter();
con=null;
pst=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:odbc:gan","scott","tiger");
int count=0;
String op1=req.getParameter("op1");
String op2=req.getParameter("op2");
String op3=req.getParameter("op3");
String op4=req.getParameter("op4");
String op5=req.getParameter("op5");
String op6=req.getParameter("op6");
String op7=req.getParameter("op7");
String op8=req.getParameter("op8");
pst=con.prepareStatement("insert into matchdetails values(?,?,?,?,?,?,?,?)");
pst.setString(1,op1);
pst.setString(2,op2);
pst.setString(3,op3);
pst.setString(4,op4);
pst.setString(5,op5);
pst.setString(6,op6);
pst.setString(7,op7);
pst.setString(8,op8);
out.println("<html><center><body>matched</body></center></html>");
int count1=pst.executeUpdate();
if(count1==0) {
out.println("<html><center><body>ENTER ALL FIELD VALUES</body></center></html>");
} else {
out.println("<html><center><body>INSERTION SUCCESFUL</body></center></html>");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (con!=null) con.close();
} catch(Exception e) {
System.out.println("error");
}
}
}
}
How can I use this path: "file:///E:/apache-tomcat-7.0.10/webapps/examples/WEB-INF/match.html" ? Is this correct?
You mean, how to access it by a webbrowser? No, that path is not correct. Tomcat listens on HTTP requests only. When started properly, it by default listens on http://localhost:8080. All webapps get their own context name which defaults to the webapp folder name. So your webapp is accessible by http://localhost:8080/examples. However, files in /WEB-INF folder are not directly accessible. You need to move the match.html one level up, in the /examples folder. Then you'll be able to access it by http://localhost:8080/examples/match.html.
Unrelated to the concrete problem, declaring connection and statement as instance variable of the servlet is a poor practice. This is not threadsafe. You need to declare them inside the method block, right before the try statement. Your HTML has also some syntax errors. Use http://validator.w3.org to learn about them. Finally, emitting raw HTML in servlets is also a poor practice, there you normally use JSPs for. But maybe you're currently just learning. Just to let you know :) Already using preparedstatements and closing the connection in finally is very good for a starter.
As to learning JSP/Servlets, I'd suggest to read our info/wiki pages as well.
JSP info page
Servlets info page
First, your html need to be a little
cleaner (see below).
Second, if you
want match.html, don't use match1
in the form action.
Third, put the
match.html file under the war
folder, not web-inf.
Fourth, you
will need to set up the web.xml
configuration file.
I did not even look yet at the servlet class, but I would suggest first to test some System.out.println() output to make sure it is even executed.
<html>
<body bgcolor="powderblue">
<center>
<h1>MATCH</h1>
<form method="post" action="/match.html">
<pre>MATCH NO <input type="text" name="op1"/></pre>
<pre>DATE <input type="text" name="op2"/></pre>
<pre>CITY <input type="text" name="op3"/></pre>
<pre>TEAM1 <input type="text" name="op4"/></pre>
<pre>TEAM2 <input type="text" name="op5"/></pre>
<pre>STADIUM <input type="text" name="op6"/></pre>
<pre>WINNER <input type="text" name="op7"/></pre>
<pre>MAN OF THE MATCH <input type="text" name="op8"/></pre>
<pre><input type="submit" value="submit"/></pre>
<input type="reset" value="reset"/>
</form>
</center>
</body>
</html>

Categories