get file path from local drive with jsp and servlet - java

I have a problem at the moment when I want to get the file path. This is my code:
public void service(HttpServletRequest request, HttpServletResponse res)
throws ServletException, IOException {
String cabArch = req.getParameter("fRutaArch");
String rutaArch = getFileName(filePart);
}
And in jsp I have this:
<td align="left" class="e2">
<input type="file" name="fRutaArch" id="fRutaArch" title="Seleccionar archivo">
</td>
<td>
<button type="submit" name="bCargar" id="bCargar">Cargar</button>
</td>
I just need the full file path, please any advice?

You can add a hidden field in the html code and modify the service function
the code follows below
<td align="left" class="e2">
<input type="file" name="fRutaArch" id="fRutaArch" title="Seleccionar archivo" onchange="document.getElementById('filepath').value=this.value" >
</td>
<td>
<button type="submit" name="bCargar" id="bCargar">Cargar</button>
</td>
<input type='hidden' name='filepath' id='filepath'/>
then the function service
public void service(HttpServletRequest request, HttpServletResponse res)
throws ServletException, IOException {
String cabArch = req.getParameter("filepath");
String rutaArch = getFileName(cabArch);
}

Related

Get checkbox values from JSP Page

I'm trying to get all the values from a list of selected checkboxs in a JSP Page, to a Java Class.
That's My JSP Page:
<table border="1" cellpadding="5" cellspacing="1" style="width: 877px; ">
<tr>
<th>Code</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
<th>Edit</th>
<th>Delete</th>
<th>Show</th>
</tr>
<c:forEach items="${productList}" var="product" >
<tr>
<td>${product.code}</td>
<td>${product.name}</td>
<td>${product.price}</td>
<td>
<input type="checkbox" name="ProductItem" value="${product.code}">
<c:url value="ShowProductList" var="url">
<c:param name="ProductItemP" value="${product.code}"/>
</c:url>
</td>
<td>
Edit
</td>
<td>
Delete
</td>
<td>
Show
</td>
</tr>
</c:forEach>
</table>
Show Items
As you can see there is a Table with a list of items with a check box in each line. At the end of the table there is button "Show Items" that triggers the user's request.
That's the servlet class that perform the request:
#WebServlet(urlPatterns = { "/ShowProductList" })
public class ShowProductList extends HttpServlet {
private static final long serialVersionUID = 1L;
public ShowProductList() {
super();
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection conn = MyUtils.getStoredConnection(request);
String[] paramValues = request.getParameterValues("ProductItemP");
System.out.println(paramValues.length);
response.sendRedirect(request.getContextPath() + "/productList");
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
When an user select two or more checkbox and click on the button "Show item" I can have only the last product code with the checkbox clicked and not all the codes with the box selected. If I Try request.getParameterValues("ProductItem"); I have a null value. I'd prefer to not have code in the JSP Page (if it is possible.)
Can someone help me find a solution ?
Thanks for your patience.
`

Hidden form in JSP/Servlet

I want to get parameters from my jsp to my servelet.
so i used input form and it works for the name and the last name but it doesn't work for my ID.
Here is the code of my JSP:
<tr>
<td><form method="post" action="ServBddInsa">
<input type="hidden" name="id" id="id" value="testId"/>
<c:out value="${ utilisateur.id }" />
</form>
</td>
<td><c:out value="${ utilisateur.prenom }" /></td>
<td><c:out value="${ utilisateur.nom }" /></td>
<td><form method="post" action="ServBddInsa">
<p>
<label for="prenom"> Prenom :</label> <input type="text"
id="prenom" name="prenom" />
<label for="nom"> Nom :</label> <input type="text" id="nom"
name="nom" />
</p>
<input type="submit" name="editer" value="editer" />
</form></td>
</tr>
</c:forEach>
and this of my servlet:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BddInsa listU = new BddInsa();
request.setAttribute("utilisateur", listU.recupererList());
this.getServletContext().getRequestDispatcher("/WEB-INF/bddinsa.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Utilisateur utilisateurs = new Utilisateur();
utilisateurs.setNom(request.getParameter("nom"));
utilisateurs.setPrenom(request.getParameter("prenom"));
utilisateurs.setId(request.getParameter("id"));
System.out.println(utilisateurs.getPrenom());
System.out.println(utilisateurs.getNom());
System.out.println(utilisateurs.getId());
BddInsa listU = new BddInsa();
listU.Editer(utilisateurs);
request.setAttribute("utilisateurs", listU.recupererList());
this.getServletContext().getRequestDispatcher("/WEB-INF/bddinsa.jsp").forward(request, response);
}
}
I get NULL when I try to see the value of id
Thank you for your help !
Please can you try to use only one open and one close form tag? I think you problem is because you have two <form method="post" action="ServBddInsa"> . So the submit button is on the second form. In this case the hidden field of the first form is not considered and it is not sent.

How i can put other img to JSP file to table

Okay, at first excuse me for my English skill.
I have a problem. Servlet load to jsp ArrayList with file address and directory address. I need to for director was one image and for files other image. Now it's look's like this pic
p.s. If you can help me with drop down list (when click on directory open and change image from close folder to open folder) I'll be very happy.
yours respectfully
JSP file:
<table border = "1">
<c:forEach items = "${fath}" var = "fath">
<th> <a href="/back?name=${fath}">
<img src="opfolder.png"> </a>
</th>
<th>
${fath}
</th>
</c:forEach>
<c:forEach items= "${FileZ}" var="FileZ">
<tr>
<td>
<a href = "open?name=${FileZ.father}">
<img src ="clfolder.png"/>
</a>
</td>
<td>${FileZ.name}</td>
</tr>
</c:forEach>
</table>`
Java Servlet file :
#WebServlet(name = "ToOut" , urlPatterns = "/fileman")
public class ToOut extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("FileZ",AllFiles.getDir(AllFiles.getAdress()));
request.getRequestDispatcher("fileman.jsp").forward(request,response);
}}

how to make an .jsp invoke a method using eclipse Google app engine

hello iam having problem invoking function in my project ![enter image description here]
<form action="register" method="post">
<table>
<tr>
<td colspan="2" style="font-weight:bold;">UserID:</td><td><input type="text" name="Id"></td>
</tr>
<tr>
<td colspan="2" style="font-weight:bold;">Name :</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td colspan="2" style="font-weight:bold;">Password</td><td><input type="password" name="pass"></td>
</tr>
<tr>
<td><input type="button" value="sumbitk"></td>
</tr>
</table>
</form>
<hr>
$
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
resp.getWriter().println("Hello, world");
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
//RequestDispatcher dispatcher;
String id=req.getParameter("id");
String name=req.getParameter("name");
String password=req.getParameter("pass");
System.out.println("user id : "+id+" name "+name+" pass "+password);
resp.sendRedirect("/index.html");
//dispatcher=getServletContext().getRequestDispatcher("/index.html");
//dispatcher.forward(req, resp);
}
}
i made form action on register and calls function post but it not seems to work when i click sumbit i dont know why .
First try write "submit" correctly in last input, if don't work try chage the type of last input to "submit" instead "button"

Passed data from JSP to Servlet returning null in Servlet

<table border="1" cellpadding="5" id="newtable">
<!-- <caption id="tablehead">Rooms are available!</caption> -->
<!-- <tr class="hover"> -->
<tr>
<th>Room No</th>
<th>AC</th>
<th>Deluxe</th>
<th>Tariff</th>
</tr>
<c:forEach var="room" items="${myrooms}">
<tr bgcolor="#4B476F" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#4B476F';">
<td class="nr">1</td>
<td name="ac"><c:out value="${room.ac}" /></td>
<td name="deluxe"><c:out value="${room.deluxe}" /></td>
<td>₹<c:out value="${room.price}" /></td>
<td><button type="button" class="mybutton" onclick="location.href='passtopayment'">Pay</button> </td>
</tr>
</c:forEach>
</table>
I want to get the td value for AC and Deluxe column on click of the corresponding row. However, when the execute the following servlet code I get, null null is printed. Please help!
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String ac = request.getParameter("ac");
String deluxe = request.getParameter("deluxe");
out.println(ac);
out.println(deluxe);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
You are using location.href='passtopayment' that is not the correct way to submit a form.
It's just a like a separate request to the Servlet nothing will be send to the Servlet.
You should use form and submit the request to the Servlet.
<form action="passtopayment" method="post">
<!-- HTML controls -->
<input type="submit" value="Submit"/>
</form>
Here is detailed Example

Categories