on click the button value should get in jsp page is null - java

HTML Page
<form action="viewParticularImage.jsp" method="post">
input type="submit" name="1" value="Details" />
input type="submit" name="2" value="Details" />
input type="submit" name="3" value="Details" />
</from>
JSP page
<%
String name = request.getParameter("name");
out.write(name);
%>
i am getting null value in name while printing the button Details in JSP Page pls help to find the solution, thanks to the replies in advance .

You have parameters named as "1", "2" and "3", but you have not parameter named as "name".
<form action="viewParticularImage.jsp" method="post">
<input type="submit" name="name" value="Details" />
...
</from>

In your JSP page make it
<%
String name = request.getParameter("1");
out.write(name);
%>

You are not sending a parameter named name in the request.That's why! To confirm, try
<%
String name = request.getParameter("1");
out.write(name);
%>
Your form should be like below. See the value of name attribute of the input text. The name attribute denotes the name of the request parameter
<form action="viewParticularImage.jsp" method="post">
First name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form>
Note : Try entering a value in the text and submit. You will receive it in the server request

I believe you haven't gained any knowledge regarding HTML ... First, go and check the tutorials regarding HTML...
Let's get to the game ... What are you trying to do ... Are you trying to pull down values set to the submit button ... or you are trying to publish a textfield where user can type their name and show that name as the result in the next page ????
If you are trying to publish the values assigned to the submit button...
HTML Page
<form action="viewParticularImage.jsp" method="post">
<input type="submit" name="1" value="Details" />
<input type="submit" name="2" value="Details" />
<input type="submit" name="3" value="Details" />
</form>
JSP page
<%
String name = request.getParameter("1");
out.write(name);
%>
If you are trying to publish what users type ...
HTML Page
<form action="viewParticularImage.jsp" method="post">
<input type="text" name="name" />
<input type="submit" value="Hit Send" />
</form>
JSP page
<%
String name = request.getParameter("name");
out.write(name);
%>
I think this clarifies you how things work !!!! Thank you ... peace !!!

Related

Why request.getParamether don't work in jsp

Hello i can't get any value from a from in my index.jsp file. I'm using tomcat7, after running index.jsp and clicking send button nothing happen. System.out.prinln() print nothinig or null ;(
index.jsp
%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<form acton="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input type="text" class="form-control" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
</body>
</html>
addnewtask.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%
String s = request.getParameter("name");
System.out.println(s);
%>
Do you know what am i doing wrong ?
This line allows you to get a parameter with its name not its id :
String s = request.getParameter("name");
Add a name to your input and correct the typo of the attribute action of your form :
<form action="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input name="name" type="text" class="form-control" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
<form>
String s = request.getParameter("xyz");
Add a name to your input box
<form action="addnewtask.jsp" method="post" >
<label for="name">Name</label>
<input type="text" class="form-control" name="xyz" id="name" placeholder="Name">
<button type="submit" class="btn btn-danger">Add</button>
<form>

Why this html button sent get request?

this code sent get request, why?
<form th:action="#{/books/edit/rename}" th:object="${book}" th:method="POST">
<input type="text" id="id" name="id" th:value="*{id}" value="1"/>
<input type="text" id="bookName" name="bookName" th:value="*{bookName}" value="nameExample"/>
<button type="submit">Rename for model</button>
</form>
Try removing the th: in the method declaration, to be as the following:
<form th:action="#{/books/edit/rename}" th:object="${book}" method="POST">
…
Because it is not reading the code and the default is GET, check this for more details https://www.w3.org/TR/html401/interact/forms.html#h-17.3

How to send hidden input parameters in Multipart form to the server

I have below HTML form. Please notice below hidden input fields have the same name (ie. "days_of_the_week").
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form enctype="multipart/form-data" name="myForm" method="post">
<input type="file" name="myfile">
<input type="hidden" name="days_of_the_week" id="day1" value="Sunday" />
<input type="hidden" name="days_of_the_week" id="day2" value="Monday" />
<input type="hidden" name="days_of_the_week" id="day3" value="Tuesday" />
<input type="hidden" name="days_of_the_week" id="day4" value="Wednesday" />
<input type="hidden" name="days_of_the_week" id="day5" value="Thursday" />
<input type="hidden" name="days_of_the_week" id="day6" value="Friday" />
<input type="hidden" name="days_of_the_week" id="day7" value="Saturday" />
<input type="submit" value="Submit">
</form>
</body>
</html>
Also assume that there can be any number of hidden fields in the HTML form with the same name or with different names.
Ex:
<input type="hidden" name="abc" id="abc1" />
<input type="hidden" name="abc" id="abc2" />
<input type="hidden" name="pqr" id="pqr1" />
<input type="hidden" name="xyz" id="xyz1" />
<input type="hidden" name="xyz" id="xyz2" />
Earlier above HTML form was not a multipart form. Therefore I could retrieve hidden input values on server side as an array using below code line.
// here request is the usual HttpServletRequest object.
String[] parameterArray = request.getParameterValues("days_of_the_week");
// Thus parameterArray = {Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
But after adding multipart/form-data attribute, now above code line returns null and Therefore parameterArray = null. So as per my understanding, hidden values are not sent to the server as usual when we add multipart/form-data attribute.
So my questions are,
Is there a way to force hidden values to be sent to the server so that we can read them from request.getParameterValues("days_of_the_week"); as usual?
If not, what is the best way to achieve above requirement? I can create a List on server side and fill it as below. But when there are any number of such hidden fields in the HTML form as described above, I may have to repeat if(item.getFieldName().equals("hidden_field_name")) condition check for each and every distinct hidden field name which is not so smart for me.
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<String> daysOfWeek = new ArrayList();
List requestList = upload.parseRequest(request);
for (Iterator iterator = requestList.iterator(); iterator.hasNext();) {
FileItem item = (FileItem) iterator.next();
if(item.isFormField()){
if(item.getFieldName().equals("days_of_the_week")){
daysOfWeek.add(item.getString());
}
}
}
I really appreciate any suggestions or solutions for the above question.

Retrieve data from JSP

When I click on a link in jspA, it will redirect to jspB with query string src. The message for src will be displayed in jspB without problem. However, why I tried to click on the submit, I am unable to retrieve the value of src in my servlet page. Is there a way for me to retrieve the value of src in servlet? Thanks.
Inside my jspB page:
<img src="<%= request.getParameter("src") %>" />
<table>
<form name="frmTest" action="test" method="post">
<input type="submit" value="sub" name="sub" />
</form>
</table>
Inside my Servlet test:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException{
String imgUrl = req.getParameter("src");
I am retrieving null value from the imrUrl.
When you submit an html form, only the input and select elements are sent as parameters. You don't have any that have a name attribute set to src.
You can use a hidden input
<form name="frmTest" action="test" method="post">
<input type="submit" value="sub" name="sub" />
<input type="hidden" name="src" value="<%= request.getParameter("src") %>" />
</form>
It is generally discouraged to use scriptlets. Read up on JSTL and EL and use those technologies instead.
I'm assuming you mean Submit from jspB? If so, you need to store the src value in a hidden field in the form so it is available when the servlet is called on submit. Something like the following
<form name="frmTest" action="test" method="post">
<input type="hidden" value="<%= request.getParameter("src") %>" name="src" />
<input type="submit" value="sub" name="sub" />
</form>
PS You should avoid using scriptlet (i.e. the code between <% and %>) and instead use jsp expression language

Using post method along with jquery modal window

I am developing an application using jquery ui and servlets.I have used modal dialog window for login.Once I Login the credentials are being sent to LoginServlet where the crendentials are checked and the user is being redirected to new page.
now Login.jsp has:
<html>
<head>
<script>
$(document).ready(function() {
$("#dialog").dialog();
</script>
</head>
<body style="font-size: 62.5%;">
<div id="dialog" title="DBoperations">
<form id="LoginForm" method="post" action="Login">
<fieldset>
<label>Username:</label>
<input type="text" id="username" value=""></input><br></br>
<label>Password:</label>
<input type="password" id="password" value=""></input><br></br>
<input type="submit" id="submit" class="submit" value="Log In" align="middle"></input>
</fieldset>
</form>
</div>
</body>
Now when I run the application the data passed to the servlet is null.I checked it using println statements.As far as I know al it takes to pass data to servlet is specifying action and using getparameter on server side...
I am gettin Null pointer exception due to the null value being passed to the login method..
why are null values passed??
Try adding a name attribute to your inputs and see if that works. I believe the browser only sends the request parameter if it has a name attribute.
<input type="text" id="username" name="username" value="">

Categories