Gettin Edit Value from index.jsp - java

I am newbie in HTML and Servlets. I have such a idex.jsp contents:
<form action="createCSV" method="get">
<input type="text" value="D:/">
<input type="submit" value="create">
</form>
And I want to use value of <input type="text" value="D:/"> inside of method Get in my Servlet:
protected void doGet( javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response )
throws javax.servlet.ServletException, IOException{}
Please, help me

change the input field in your html form like this:
<input type="text" name="userInput" value="D:/">
inside the servlet do that:
String userInput = request.getParameter("userInput");

Related

How to use inputs and post method

I'm starting to learn java web, and i need some insight on why my code isn't working
when i try to submit my code i notice that when i go to the pages where should be an alteration it doesn't show anything.
i'll show some code
Post Method
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String titulo = request.getParameter("titulo");
String imagem = request.getParameter("imagem");
String descricao = request.getParameter("descricao");
String categoria = request.getParameter("categoria");
Noticias noticia = new Noticias();
System.out.println(titulo);
noticia.setTitulo(titulo);
System.out.println(descricao);
noticia.setDescricao(descricao);
System.out.println(categoria);
noticia.setCategoria(categoria);
System.out.println(categoria);
noticia.setImagem(imagem);
Noticias.dados.add(noticia);
response.sendRedirect(categoria);
}
Get Method
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
Inputs
<form>
<div class="form-group">
<label for="titulo">Titulo</label>
<input class="form-control" id="titulo" placeholder="Titulo">
</div>
<div class="form-group">
<label for="imagem">Imagem</label>
<input class="form-control" id="imagem" placeholder="Imagem">
</div>
<div class="form-group">
<label for="descricao">Descrição</label>
<input class="form-control" id="descricao" placeholder="Descrição">
</div>
<div class="form-group">
<label for="categoria">Categoria</label>
<input class="form-control" id="categoria" placeholder="Categoria">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
my sysout's dont show any output.
and it doesn't show any alteration on the pages, i guess post isn't being initialized
If you are submitting form you have to mention method and action attributes into form tag.
<form action="servletname" method="post">
That servlet should be present into web.xml too.
You can follow this link https://www.journaldev.com/1877/servlet-tutorial-java

convert parameter from multiselect checkbox of jsp page

<input type="checkbox" name="premium" value="HBO">HBO <br>
<input type="checkbox" name="premium" value="FOXP">FOX Película <br>
<input type="checkbox" name="premium" value="FOX">FOX + <br><br>
<input type="submit" value="cotizar" name="Cotizar" />
<br><br>
I'm trying to bring from an index.jsp the premium parameter of a checkbox, in a servlet of a client of a web service. I have to save it as a list but what I have tried has not worked
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String plan = request.getParameter("plan");
String PREMIUM = request.getParameter("premium");
How do I convert request.getParameter ("premium") into a list?
Try this:
String[] premiums = request.getParameterValues("premium");
You need to put all the checkbox inside of form then only multiple values will be sent to server.
HTML:
<form method="post">
<input type="checkbox" name="premium" value="HBO">HBO <br>
<input type="checkbox" name="premium" value="FOXP">FOX Película <br>
<input type="checkbox" name="premium" value="FOX">FOX + <br><br>
<input type="submit" value="cotizar" name="Cotizar" />
</form>
Servlet:
To get multiple values we need to use getParameterValues(), if you any value for the given parameter name it will give string array of values. If we have one value it will given string array with length of 1.
String[] premiums = request.getParameterValues("premium");
If no value sent to server then above method will return null value.

Reading table html rows values to a servlet to another jsp page for editing

I want to get the values for the rows which is then sent to a Java servlet then read by another page and inserts those values into the text boxes for the user to edit and write it back into the text file.
So it gets read by ProductIO which reads the textfile.
Its then entered into a jsp table
<c:forEach var="product" items="${products}">
<tr>
<td ><c:out value='${product.code}'/></td>
<td ><c:out value='${product.description}'/></td>
<td >${product.priceCurrencyFormat}</td>
<td><form action="editproduct" method="post">
<input type="submit" value = "Edit">
</form>
</td>
<td><form action="deleteproduct" method="post">
<input type="submit" value = "Delete">
</form>
</td>
</tr>
</c:forEach>
The User Clicks either the delete or edit button which will then send action to either the deleteproduct servlet or the editproduct servlet(only asking about the edit for now)
The edit product servlet
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String url = "/editproduct.jsp";
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
String action = request.getParameter("action");
if (action == null) {
action = "editproduct"; // default action
} else if (action.equals("editproduct")) {
String productCode = request.getParameter("productCode");
String descString = request.getParameter("description");
//HttpSession session = request.getSession();
Product product = (Product) session.getAttribute("cart");
if (product == null) {
product = new Product();
}
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
}
Which the three Values are put into three text boxes on the editProduct.jsp page(where im having a problem getting the values to insert into the text boxes for it to be written back to the text file with the new information)
<form action="Product" method="post" >
<input type="hidden" name="action" value="add">
<label>Code:</label>
<input type="text" name="code" value='<%=request.getAttribute("code")%>'
required><br>
<label >Description:</label>
<input type="text" name="desc" value='<%=request.getAttribute("description")%>'
required ><br>
<label >Price:</label>
<input type="text" name="price" value='<%=request.getAttribute("price")%>'
required ><br>
<label> </label>
<!--<input type="submit" value="Update Product" class="margin_left">-->
<!--<input type="submit" value="View Product" class="margin_left">-->
<button type="submit">Update</button><br>
I can share more code if needed.
You aren't calling request.setAttribute() with any of the attributes in your Servlet. I assume you meant to add something like
request.setAttribute("code", productCode);
request.setAttribute("description", descString);
request.setAttribute("price", product.getPrice());
Before forwarding the request.

perform a servlet doPost from jsp form file

I'm new to servlets and jsp files, and I encountered the following problem:
I have the following jsp form file:
<FORM action="http://myApp/register" method="post">
<P>
First name: <INPUT type="text" name="firstname"><BR>
Last name: <INPUT type="text" name="lastname"><BR>
email: <INPUT type="text" name="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>
and a servlet which handles a doPost request on the url above.
I want to give the values of the form (such as the first name and last name) to the doPost request.
Any ideas?
Thanks a lot! :)
You can get all the parameters by their name as request.getParameters("firstname") to get the value of the input fields.
public class ServletClass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String firstName = request.getParameter("firstname");
String lastName = request.getParameter("lastName");
// and so on.....
}
}

How to pass a session value as request parameter from JSP to servlet?

<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");
.......
}

Categories