I am new to JSP/Servlet.
I want to use one servlet for handling different request sent from different JSP form/button.
For example
student.jsp =>
<form action="SingleController?action=student" method="POST">
Student ID: <input type="text" name="stid">
Name: <input type="text" name="stname">
<input type="submit" value="Submit" name="student">
</form>
teacher.jsp =>
<form action="SingleController?action=teacher" method="POST">
Teacher ID:<input type="text" name="tid">
Name: <input type="text" name="tname">
<input type="submit" value="Submit">
</form>
SingleController
How can I manage these two different requests using a single Servlet ?
use if conditions in servlet on action .
String action = request.getParameter("action");
if (action.equals("student")){
// to do rest of code..
}
else if (action.equals("teacher")){
// to do rest of code..
}
Related
I am developing a WebApp which uses Spring-MVC & Thymeleaf as template engine.
In my HTML,I got a form like this
<form action="/submit" method="POST">
First name: <input type="text" name="fname"/>
Last name: <input type="text" name="lname"/>
<input type="text" id="taskId" name="taskId" th:value="${taskId}" />
<input type="submit" value="Submit"/>
</form>
And I have a controller
#RequestMapping("/submit")
public void addUser(String taskId, String fname, String lname, ModelMap map) {
//save to db...
}
At first I expect taskId, fname, lname will contains the value from the form, but they are all null
Finally I try to wrap all the input field by <fieldset>tag, and it suddenly works. i.e:
<form action="/submit" method="POST">
<fieldset>
First name: <input type="text" name="fname"/>
Last name: <input type="text" name="lname"/>
<input type="text" id="taskId" name="taskId" th:value="${taskId}" />
<input type="submit" value="Submit"/>
</fieldset>
</form>
I would like to know:
Why adding <fieldset> will make everything works?
The HTML - <fieldset> tag is used to group related elements in a form.
Basically the <fieldset> tag draws a box around the related elements in a form.
Most online forms are poorly designed, disorganized, and hard to use. One thing you can do to make your forms better is to organize them into logical sections. The <fieldset> element provides an easy way to break forms down into logical sections.
For more information you can always got to - https://html.com/tags/fieldset/
My index.jsp is as following:
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="${fname}"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
with value=${fname} is get from dopost method in servlet when form summited
request.setAttribute("fname", fileName);
getServletContext().getRequestDispatcher("/index.jsp").forward(
request, response);
But it's weird that when I deployed index.jsp
My text field always show ${fname} in text area, even after form submitted, its still get that value (correctly it must show 'filename')
Does anyone meet this problem like me?
First thing is that, you are using wrong syntax to display value in JSP.
Below syntax is wrong.
value="${fname}"
We use expression tag fordisplaying value in JSP page. Expression tag convert into Java statement.
<%=request.getAttribute("fname")%>
When you first time open your index.jsp page, it will show you blank value and when request will come back from server side, then it will show you correct result.
Use below code.
<%if(request.getAttribute("fname")!=null){%>
<input type="text" value ="<%=request.getAttribute("fname") %>"/>
<%}else
{ %>
<input type="text"/>
<%} %>
In JSP, the correct syntax to retrieve the values is
<%=request.getAttribute("fname")%>
This is known as assignment OR expression tag (<%=%>).
Try this ...
<form action="FileUploadServlet" id="formSubmit" method="post" enctype="multipart/form-data">
<input type="text" id="txtFileName" value="<%=request.getAttribute("fname")%>"/>
<input type="file" name="fileName" id="selectedFile" style="display: none;">
<input type="button" id="btnBrowse" value="Browse..." onclick="document.getElementById('selectedFile').click();" />
<input type="submit" value="Upload" id="btnUpload">
</form>
More related to this, here.
i have created a java project in eclipse,in this project i have an html form customer.html as shown below
<form>
Name :<input type="text" id="name"/>
Address: <input type="text" id="add"/>
Age:<input type="int" id="age"/>
<input type="submit" onclick="what should i call here in my program"/>
</form>
once the user fills the form and submits it i want to access the form field in java code is it possible.if so how or else is there anyother solution to create an xml file for this form
<input type="submit" onclick="only Javascript function can be called here"/>
if you want to access html form elements at java code you need to submit your form at some controller or another jsp. Like..
<form name="new_employee" method="post" action="../admin/newEmployee.do">
Name :<input type="text" name="employeeName"/>
Address: <input type="text" name="add"/>
Age:<input type="text" name="age"/>
<input type="submit" value="Submit"/>
</form>
form elements can be accessed from generated request by their names like..
request.getParameter("employeeName");
<c:forEach items="${movieList}" var="movie" varStatus="status">
<tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
<td>${movie.title}</td>
<td>${movie.genre}</td>
<td>${movie.year}</td>
<td>${movie.boxoffice}</td>
<td>
<form:form action=edit.htm>
<input type="hidden" name="edit" value="movie name">
<input type="submit" value="Edit">
</form:form>
<form:form action=delete.htm>
<input type="hidden" name="delete" value="movie name">
<input type="submit" value="Edit">
</form:form>
</tr>
</c:forEach>
This is the section of code I have at the moment. The idea is to display movie data, but also provide buttons to send the user to either a filled form page to edit the data or simply delete the respective data and redirect to the same page. I am just unsure as how to pass along the movie object. I know it is simple, but I can't find the reference I used previously...
Thanks.
U can try this :
put this object in a hidden input
<input type="hidden" id="hidden_object" value="" name="hidden_object"></input>
than you can access the value with the method getParameter()
OR
put this object in your session
in your jsp
<% session.setAttribute("movie", ${movie}); %>
good luck
I am writing application on playframework. I have a search form and I want to send POST request and be able to accept it in my controller, how this is possible?
There Many samples for it, First Add a method login in your Application (or whatever) controller.
Lets assume your posting login details for your controller.
public static void login(String userCode,String password){
User loginUser = User.find("byUserCodeAndPassword",userCode,password).first();
if(loginUser == null){
flash.put("username",userCode);
flash.error("Invalid Credentials");
index();
}
else{
Cache.set(session.getId(),loginUser,"20mn");
Home.Home();
}
}
In your conf/routes file add
POST / Application.login
Assuming you have a index.html in your app/views/Application folder.
From the Html Page:
<div id="login">
#{form #login(), id:'formLogin'}
<p class="field">
<label>User Code:</label>
<input type="text" name="userCode" size="19" value="${flash.userCode}" required>
</p>
<p class="field">
<label>Password:</label>
<input type="password" name="password" size="19" required>
</p>
<p class="buttons">
<input type="submit" value="Login">
</p>
#{/form}