This question already has answers here:
NullPointerException, when reading HTML input [duplicate]
(3 answers)
Closed 6 years ago.
home.jsp
<form method="POST" action="Initiater.do">
<table>
<tr>
<td>
Internal Diameter from FlowAss:
</td>
<td>
<input type="text" id="Id" />
</td>
<td>
Depth:
</td>
<td>
<input type="text" id="Depth" />
</td>
<td>
Units:
</td>
<td>
<select>
<option value="ft">feet</option>
<option value="mts">meters</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
Home.java
public class Home extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
System.out.println("in Post");
String id = (String) request.getParameter("Id");
String depth = request.getParameter("Depth");
/*double id =Double.parseDouble(request.getParameter("Id"));
double depth =Double.parseDouble(request.getParameter("Depth"));*/
System.out.println("Id"+id);
System.out.println("Depth"+depth);
}
the servlet Home.java's doPost method is called, the values id and depth are returning nulls when I debug can anyone help me out with this.
Add name attribute to the input tag.
Change
<input type="text" id="Depth" />
<input type="text" id="Id" />
To
<input type="text" id="Depth" name="Depth"/>
<input type="text" id="Id" name="Id"/>
Request.getParameter gets data by name attribute and not id
Related
I am using this code to get form data used inside jsp Form.
String product=request.getParameter("product");
String qty=request.getParameter("quantity");
String price=request.getParameter("price");
writer.println(product +" "+qty+" "+price);
How Ever i am using dynamic form.How do i can get all datas in controller class.
Here is the code of view class i.e jsp Pages.
<script>
$(document).ready(function(){
$("button").on("click", function(){
var row = '<tr><td><input type="text" name="product" value="product..">'+
'</input></td><td><input type="number" name="quantity" value="quanty..">' +
' </input></td><td><input type="number" name="price" value="price.."> </input></td></tr>';
$("#customers").append(row);
});
});
</script>
</head>
<body>
<button type="button">Add More Products</button>
<form action="XmlServlet" method="get">
<table id="customers">
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td><input type="text" name="product" value="product.."></input> </td>
<td><input type="number" name="quantity" value="quanty.."></input></td>
<td><input type="number" name="price" value="price.."></input></td>
</tr>
<tr>
<td><input type="submit"></td>
</tr>
</table>
</form>
How do i can get all data's saved in text field after add more button action's.
String[] products = request.getParameterValues("product");
JSP page code:
<c:forEach items="${studentInfoList}" var="studentInfo">
<tr>
<td>${studentInfo.student.surname} ${studentInfo.student.name} ${studentInfo.student.patronymic}</td>
<td> ${studentInfo.group.name} </td>
<td> ${studentInfo.semester.getYear()} </td>
<td> ${studentInfo.speciality.title} </td>
<td>
<form name="changeStudent" method="POST" action="controller">
<input type="hidden" name="command" value="openChangeStudentPage"/>
<imput type="hidden" name="studentId" value="${studentInfo.student.id}"/>
<input type="submit" value="Change"/>
</form>
</td>
</tr>
</c:forEach>
Code generated in browser:
<tr>
<td>Surname Name Patr</td>
<td> KV-01 </td>
<td> 4 </td>
<td> Computer eng. </td>
<td>
<form name="changeStudent" method="POST" action="controller">
<input type="hidden" name="command" value="openChangeStudentPage"/>
<imput type="hidden" name="studentId" value="2"/>
<input type="submit" value="Change"/>
</form>
</td>
</tr>
Now at servlet I try to get "studentId" field by following code and add to request student field:
int studentId = Integer.parseInt(request.getParameter("studentId"));
Student student = DAOFactory.getInstance().getStudentDAO().findStudentById(studentId);
request.setAttribute("student", student);
But on line int studentId = Integer.parseInt(...) occurs error:
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:454)
java.lang.Integer.parseInt(Integer.java:527)
ua.kpi.fpm.portal.command.administrator.OpenChangeStudentPageCommand.execute(OpenChangeStudentPageCommand.java:26)
ua.kpi.fpm.portal.controller.Controller.processRequest(Controller.java:53)
ua.kpi.fpm.portal.controller.Controller.doPost(Controller.java:85)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
I can't find out why this error occures and why studentId from form can be null.
It should be
<input type="hidden" name="studentId" value="${studentInfo.student.id}"/>
instead of
<imput type="hidden" name="studentId" value="${studentInfo.student.id}"/>
Look at imput tag that is not valid.
String isno1=request.getParameter("isbn");
String bktitle2=request.getParameter("booktitle");
String authr3=(String) request.getParameter("author");
System.out.println(isno1+bktitle2+authr3);
Enumeration paramaterNames = request.getParameterNames();
when i am taking the parameters in servlet then i am gettin my values as 'null'
what wrong am i doing.
this is the way i am setting the parameters...
from a jsp page using script tag
<script type="text/javascript">
function getHTTPObject()
{
var httpobject;
if(!httpobject && typeof(XMLHttpRequest) != 'undefined')
{
try{
httpobject=new XMLHttpRequest();
}
catch(e){
xmlhttp=false;
}
}
return httpobject;
}
var httpob=getHTTPObject();
function handleHttpResponse(){
if(httpob.readyState==4){
//alert("sd");
var result=httpob.responseText;
alert(result);
/* document.write("hi your book is submitted !!!!!"); */
}
}
function auth(){
var params="isbn="+document.mayurform.isbn.value+"&booktitle="+document.mayurform.booktitle.value+"&author="+document.mayurform.author.value;
alert("params sending"+params);
httpob.open("POST","addbook",true);
httpob.setRequestHeader("Content-type","application/x-www-form-urlencoded");
httpob.setRequestHeader("Content-length",params.length);
httpob.setRequestHeader("Connection","close");
/* httpob.setRequestHeader("Content-type","application/x-www-from-urlencoded");
httpob.setRequestHeader("Content-length",params.length);
httpob.setRequestHeader("Connection","close"); */
httpob.onreadystatechange=handleHttpResponse;
httpob.send();
}
</script>
and this my form.....
<form style="margin: 100px;" name="mayurform">
<table align="center">
<tr>
<td align="center">ISBN NO.</td>
<td><input align="middle" type="text" size="20" name="id" id="isbn">
</tr>
<tr>
<td align="center">Book-Title</td>
<td><input align="middle" type="text" size="20" name="pwd" id="booktitle">
</td>
</tr>
<tr>
<td align="center">Author</td>
<td><input align="middle" type="text" size="20" name="pwd" id="author">
</tr>
<tr>
<td><input align="middle" type="button" size="20" name="Add-Book" onclick="auth()">
</tr>
</table>
</form>
You are fetching parameters with ID's you should give Names.
for example
String isno1=request.getParameter("isbn"); //here is isbn is id
you should write
<input align="middle" type="text" size="20" name="id" id="isbn">
String isno1=request.getParameter("id");-----------^
and also
<td><input align="middle" type="text" size="20" name="pwd" id="booktitle">
<td><input align="middle" type="text" size="20" name="pwd" id="author">
both inputs have the same **name** please check it
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
Use like below ajax send() to send data
function auth(){
var params="isbn="+document.mayurform.isbn.value+"&booktitle="+document.mayurform.booktitle.value+"&author="+document.mayurform.author.value;
alert("params sending"+params);
.......
...
httpob.send(params);//change is here
}
call httpob.send(),passing in the parameters that will be sent (without the "?" prefix).
I have the following class that I'm using in my Java with JSP applicaton.
//
public class QuestionBO implements Serializable{
private int questionId;
private int testID;
private String question;
private TutorBO infoAboutTutor;
private SubjectBO infoAboutSubject;
private TestBO infoAboutTest;
private List<AnswerBO> answers;
public QuestionBO() {
}
public QuestionBO(String question) {
this.question = question;
}
getter & setter....
The JSP page has a form where each Question (its String representation) has a checkbox next to it. A user marks some of the questions and submits the form to the server for processing by a servlet.
What is the conventional way of binding the Question objects with the checkboxes so that I could find out what Questions have been selected?
Currently I'm using the following approach for constructing the form:
//
<c:if test="${not empty questionsForSubject}">
<form action="/TutorWebApp/controller" method="POST" name="addQuestionForm">
<input type="hidden" name="command" value="add_question_list" />
<input type="hidden" name="testName" value="${testName}"/>
<table border ="1">
<tbody>
<c:forEach items="${questionsForSubject}" var="question">
<tr>
<td>
<input type="checkbox" name ="choosen_question"
value="${question.getQuestion()}">
${question.getQuestion()}
<br />
</td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Add questions "/>
</form>
And I shouldn't use frameworks.
Thanks
And I have last question
<c:if test="${not empty questionsForSubject}">
<form action="/TutorWebApp/controller" method="POST" name="addQuestionForm">
<input type="hidden" name="command" value="add_question_list" />
<input type="hidden" name="testName" value="${testName}"/>
<input type="hidden" name="questionsForSubject" value="${questionsForSubject}"/>
<table border ="1">
<tbody>
<c:forEach items="${questionsForSubject.keySet()}" var="questionID">
<tr>
<td>
<input type="checkbox" name ="choosen_question" value="${questionID}">
${questionsForSubject.get(questionID).getQuestion()}
<br />
</td>
</tr>
</c:forEach>
</tbody>
</table>
<input type="submit" value="Добавить вопросы"/>
</form>
How I can get map from this page on servlet?
Give each checkbox an unique value. For example, the unique question identifier:
<c:forEach items="${questionsForSubject}" var="question">
<tr>
<td>
<input type="checkbox" name="chosen_question" value="${question.questionId}" />
${question.question}
<br />
</td>
</tr>
</c:forEach>
This way you'll be able to grab all checked values by just the following call in the servlet:
String[] chosenQuestions = request.getParameterValues("chosen_question");
Generate an unique name for each checkbox as follows:
<input type="checkbox" name="${question.questionId}" />
or:
<input type="checkbox" name="choosen_question_${question.questionId}" />
After that, you're already able to identify each checkbox in your servlet
I have a login page with form action of j_security_check. As of now this form just have two fields namely username
and password. I want to add a new dropdown to this form and collect the selected value in controller using
#RequestParam. For some reason I am not able to pass this dropdown value from JSP to my controller as its throwing the
exception: MissingServletRequestParameterException (Which occurs anytime a request param is missing).
In the code below I added the Visuals dropdown. Do I need to use Spring:Bind tag here?
Also on successful login, the control is directed to a controller with request mapping /controller1.html and this is where
I am trying to collect the dropdown value.
<form name="appLogin" action="j_security_check" method="POST">
<table width="100%">
<tr>
<td align="center">
<table>
<tr>
<td>Username: </td>
<td><input id="userName" name="j_username" value=""/></td>
</tr>
<tr>
<td>Password: </td>
<td><input name="j_password" type="password" value="" /></td>
</tr>
<tr>
<td>Visual: </td>
<td><Select name="visuals" id="visuals"/>
<option value="S1">S1</option>
<option value="S2">S2</option>
<option value="S3">S3</option>
<option value="S4">S4</option>
</Select>
</td>
</tr>
</table>
<table>
<tr>
<td>
<button type="submit" name="submit" value="Sign In">Sign In</button>
<input type="submit"/>
</td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
</form>
Controller Code:
#RequestMapping( value = " /controller1.html", method = RequestMethod.GET )
public String setupForm( #RequestParam(value = "visuals", required=false) String visuals,
ModelMap model )
{
List<String> studentNames = new ArrayList<String>();
List<String> teacherNames = new ArrayList<String>();
model.addAttribute("someData", teacherNames);
model.addAttribute("anotherData", studentNames);
model.addAttribute("visuals", visuals);
log.info("Role from Dropdown: " + visuals);
return "school/classTen";
}
You need to create yyour own Filter by extending AbstractAuthenticationProcessingFilter
I don't have the entire code in front of my eyes, but the following article could help you:
http://mark.koli.ch/2010/07/spring-3-and-spring-security-setting-your-own-custom-j-spring-security-check-filter-processes-url.html