EDIT: I've discovered that my form is not passing any parameters on submit, I'm not sure why that would be.
I have a servlet class:
#MultipartConfig
public class TrainingServlet extends HttpServlet { .. }
Inside I have my doPost method:
public void doPost(HttpServletRequest req, HttpServletResponse resp) {
String reqParm = req.getParameter("handler");
...
if ("parsefile".equals(reqParm)) {
Part part = req.getPart("file");
new ParseHotelFile().ParseHotelFile(part);
getServletContext().getRequestDispatcher("/training/uploadhotels.jsp").forward(req,resp);
}
}
My problem is, whenever the line "Part part = req.getPart("file");" is called, a nullpointerexception is thrown; I can't figure out why. I checked my servlet version and it's 3.0.
Here's my html form:
<form method="post" enctype="multipart/form-data" action="/trainingservlet?handler=parsefile">
<table style="background-color:f0f0f0; padding:8px; border:1px solid #80bfff">
<tr>
<td>File to upload: <input style="background-color:#e0e0e0; border:1px solid #a0a0a0" type="file" name="file"></td>
</tr>
<tr>
<td><br><input type="submit" value="Import"></td>
</tr>
</table>
</form>
Related
I have the following situation:
There is my jsp file
<html>
<head>
<title>car.by</title>
</head>
<body>
<form method="get" action="${pageContext.request.contextPath}/editServlet">
<button type="submit">Редактировать(для админа)</button>
</form>
<form method="get" action="${pageContext.request.contextPath}/">
<button type="submit">Выйти</button>
</form>
<form method="get" action="${pageContext.request.contextPath}/">
<tr>
Марка <br>
Коробка передач <br>
Логин: <br>
<button type="submit">Показать</button> <br>
</tr>
</form>
<jsp:useBean id="cars" scope="request" type="java.util.List"/>
<c:forEach items="${cars}" var="car" varStatus="status">
<tr>
<img SRC=${car.c_photo} width="250" height="250"> <br>
Модель:<td>${car.c_model}</td><br>
Цена за день:<td>${car.c_price}</td><br>
Скидка:<td>${car.p_sale}</td><br>
Производитель:<td>${car.c_producer}</td><br>
Коробка передач:<td>${car.c_transmission}</td><br>
Место нахождения:<td>${car.c_owner}</td><br>
<form method="get" action="${pageContext.request.contextPath}/reservationServlet">
<button style="background-color: black" type="submit">Заказать</button>
</form>
</tr>
</c:forEach>
</body>
</html>
As you can see, here I have a list of forms. Each form has button. I don’t understand, how can I pass for example the car.c_model variable value from the jsp to servlet. Here is my servlet:
#WebServlet("/mainServlet")
public class MainServlet extends HttpServlet{
private CarManager carManager = new CarManager();
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
List<Car> cars = carManager.getCarInfo();
req.setAttribute("cars", cars);
RequestDispatcher dispatcher = req.getRequestDispatcher("MainPage.jsp");
dispatcher.forward(req, resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
I will appreciate any help, thanks in advance!
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.
`
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);
}
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"
<c:forEach var="it" items="${sessionScope.projDetails}">
<tr>
<td>${it.pname}</td>
<td>${it.pID}</td>
<td>${it.fdate}</td>
<td>${it.tdate}</td>
<td> Related Documents</td>
<td>${it.pdesc}</td>
<form name="myForm" action="showProj">
<td><input id="button" type="submit" name="${it.pID}" value="View Team">
</td>
</form>
</c:forEach>
Referring to the above code, I am getting session object projDetails from some servlet, and displaying its contents in a JSP. Since arraylist projDetails have more than one records the field pID also assumes different value, and the display will be a table with many rows.
Now I want to call a servlet showProj when the user clicks on "View Team" (which will be in each row) based on the that row's "pID".
Could someone please let me know how to pass the particular pID which the user clicks on JSP to servlet?
Instead of an <input> for each different pID, you could use links to pass the pID as a query string to the servlet, something like:
View Team
In the showProj servlet code, you'll access the query string via the request object inside a doGet method, something like:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String pID = request.getParameter("pID");
//more code...
}
Here are some references for Java servlets:
HttpServletRequest object
Servlet tutorials
Pass the pID along in a hidden input field.
<td>
<form action="showProj">
<input type="hidden" name="pID" value="${it.pID}">
<input type="submit" value="View Team">
</form>
</td>
(please note that I rearranged the <form> with the <td> to make it valid HTML and I also removed the id from the button as it's invalid in HTML to have multiple elements with the same id)
This way you can get it in the servlet as follows:
String pID = request.getParameter("pID");
// ...
Define onclick function on the button and pass the parameter
<form name="myForm" action="showProj">
<input type='hidden' id='pId' name='pId'>
<td><input id="button" type="submit" name="${it.pID}" value="View Team" onclick="populatePid(this.name)">
</td>
.....
Define javascript function:
function populatePid(name) {
document.getElementById('pId') = name;
}
and in the servlet:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String pID = request.getParameter("pId");
.......
}