Linking HTML to JSP page - java

I'm have problems linking my HTML and JSP. Just trying to make a simple html page to take a "email" and "password" then have the jsp display on the next page. But when i click submit the parameters dont get passed i just get "null", any ideas where im going wrong?
HTML Page
<html>
<head>
<title>Enter your email and password</title>
</head>
<body>
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" email=email></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" password=password></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
JSP Page
<html>
<body>
<%
// get info from request
String email = request.getParameter("email");
String password = request.getParameter("password");
%>
<p>Here is the info you provided:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">Email:</td>
<td><%= email %></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><%= password %></td>
</tr>
</table>
<form action= "Next" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>

You need to use the name attribute in the form fields .
<input type="text" name="email"></td>
The value of "email" can be retrieved in JSP as :
String email = request.getParameter("email");
OR
${param.email}
OR
<c:out value="${param.email}"/> <%--(Using JSTL)--%>
Here are the complete list of attributes.
name = string #
The name part of the name/value pair associated with this element for the purposes of form submission.

You should specify the input name attribute. Try this,
<form action="form.jsp" method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">Email:</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
For more info you can try this link

Related

How to generate a download request from html page to controller of spring mvc

<html>
<head>
<title>Student Registration</title>
</head>
<body>
<h3 align="center">New Trainee Registration</h3>
<table align="center" cellpadding="10">
<!-- First Name -->
<tr>
<td>First Name</td>
<td>${traineeData.fName}</td>
</tr>
<!-- Last Name -->
<tr>
<td>Last Name</td>
<td>${traineeData.lName}</td>
</tr>
<!-- Father's Name -->
<tr>
<td>Father's Name</td>
<td>${traineeData.fatherName}</td>
</tr>
<!-- Mobile Number -->
<tr>
<td>Mobile No.</td>
<td>${traineeData.mobile}</td>
</tr>
<tr>
<td>Joining Date</td>
<td>${traineeData.joiningDate}></td>
</tr>
<tr>
<td>Grade</td>
<td>${traineeData.grade}</td>
</tr>
<tr>
<td>Gender</td>
<td>${traineeData.gender}</td>
</tr>
<!-- Course -->
<tr>
<td>COURSES<br />APPLIED FOR
</td>
<td>${traineeData.course}</td>
</tr>
<tr>
<td>Joining Date</td>
<td>${traineeData.joiningDate}</td>
</tr>
<!-- Submit and Reset -->
<tr>
<td colspan="2" align="center">
<button type="button" >Generate Certificate</button>
</td>
</tr>
</table>
</body>
</html>
Let's say, I have a download button on my HTML page. when clicking on this button a request goes through the controller of the spring MVC framework along with "enrollmentNo".
On Controller class, I will use this enrollmentNo.
I have a class which will generate a certificate for the student by fetching data using the student's enrollmentNo.
and response goes back and a file will download(which a pdf file generated by my pre-created class)
I have tried with many ways but not get what I want.
#RequestMapping(value = "/generateCertificate")
public void generateCertificate(#RequestParam("traineeData.enrollmentNo") int enrollmentNo) {
System.out.println("you in /generateCertificate");
// I don't know what should I return...
// please change return type accordingly...
}
I want a button on my HTML page named "Generate certificate". The code should be easy and normal, Ajax can be used here.
Your button isn't actually doing anything, useful.
Try this code around your button...
<tr>
<td colspan="2" align="center">
<form action="/generateCertificate" method="GET">
<input type="hidden" name="enrollmentNo" value="${traineeData.enrollmentNo}"
<button type="submit">Generate Certificate</button>
</form>
</td>
</tr>
Then your controller will need to changed slightly...
#RequestMapping(value = "/generateCertificate")
public void generateCertificate(#RequestParam("enrollmentNo") int enrollmentNo) {
// The #RequestParam was changed to the name of the param from the form and not the object/value.
}

Get data from jsp to another

hello everyone i've been looking how get data from a list from a jsp and put it into another jsp with a form but i dont know how can i get the id from the list and send it to my form :/ btw im working on netbeans
this is my list.jsp that shows on screen all the errors registered on db
<h1>List of Errors</h1>
<%
ErrorDAO daoaut=new ErrorDAO();
List<ErrorENT> list=daoaut.list();
%>
<table BORDER="3">
<tr>
<td>ID</td><td>DATE</td><td>LOG</td><td>ESTATE</td><td> </td>
</tr>
<% for(ErrorENT aut:list){ %>
<tr>
<td> <%= aut.getIdError()%> </td>
<td> <%= aut.getDate()%> </td>
<td> <%= aut.getLog() %> </td>
<td> <%= aut.isEstate() %> </td>
<td><input type="button" id="btnUpdate" value="Update"/></td>
</tr>
<% } %>
</table>
and somehow when i click on the button Update it should get the id from the row and pass it to my form, but i dont know how to get the id and how it should be recived from the other side :(
Using Hidden Tag Or using anchor tag
<h1>List of Errors</h1>
<%
ErrorDAO daoaut=new ErrorDAO();
List<ErrorENT> list=daoaut.list();
%>
<table BORDER="3">
<tr>
<td>ID</td><td>DATE</td><td>LOG</td><td>ESTATE</td><td> </td>
</tr>
<% for(ErrorENT aut:list){ %>
<form action="Your another jsp" method="post"> // using form for submit to another jsp
<tr>
<td> <%= aut.getIdError()%> </td>
<td> <%= aut.getDate()%> </td>
<td> <%= aut.getLog() %> </td>
<td> <%= aut.isEstate() %> </td>
<td><input type="hidden" value="<%=aut.getIdError()%>" name="id"/></td>
<td><input type="button" id="btnUpdate" value="Update"/></td>
</tr>
</form>
<% } %>
</table>
// now you can IdError from next page using request.getParameter("id");
2 ND METHOD USING anchor tag
<%
ErrorDAO daoaut=new ErrorDAO();
List<ErrorENT> list=daoaut.list();
%>
<table BORDER="3">
<tr>
<td>ID</td><td>DATE</td><td>LOG</td><td>ESTATE</td><td> </td>
</tr>
<% for(ErrorENT aut:list){ %>
<tr>
<td> <%= aut.getIdError()%> </td>
<td> <%= aut.getDate()%> </td>
<td> <%= aut.getLog() %> </td>
<td> <%= aut.isEstate() %> </td>
<td> <input type="button" id="btnUpdate" value="Update"/> </td>
</tr>
</form>
<% } %>
</table>
Actually I prefer to use Servlets or some MVC framework (SpringMVC, Struts2) instead.
However if you want to achieve your target with just using JSPs:
In list.jsp :
//use this :
<td><a href="/otherJsp.jsp?idError=<%= aut.getIdError() %>" > Update </a></td>
//instead of :
<td><input type="button" id="btnUpdate" value="Update"/></td>
In the otherJsp.jsp
//Retrieve the Error ID from request
The Error ID is : <%= request.getParameter("idError") %>

Redirecting to a servlet from a JSP on button click

Am trying to link from a JSP to a servlet . On clicking the button with the name="conf" I need to redirect to a servlet "/Initial" . The problem is when I use type="button" nothing happens, while when I use type="submit" the page gets directed to servlet "/Initial" and does the action there. Am not able to identify the problem.
Here is my code:
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%# page import="reg.serv.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="post">
<center>
<table border="1" width="30%" cellpadding="3">
<thead>
<tr>
<th colspan="2">Register Here</th>
</tr>
</thead>
<tbody>
<tr>
<td>Username</td>
<td><input type="text" class="" id="username" name="username1" value="" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password1" id="password" value="" /></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="confirmpassword1" id="confirmpassword" value="" /></td>
</tr>
<tr>
<td>Mobile Number</td>
<td><input type="text" class="" id="mob" name="mob1" value="" /></td>
</tr>
<tr>
<td>Email ID</td>
<td><input type="text" class="" id="email" name="email1" value=" " /></td>
</tr>
<tr>
<td>Address</td>
<td><textarea id="address" name="address1"></textarea></td>
</tr>
<tr>
<td colspan="2">Already registered Login Here</td>
</tr>
</tbody>
<tr>
<td><input type="button" value="confirm" name="conf" /></td>
<td><input type="reset" value="Reset" /></td>
<td><input type="button" value="Cancel" name="Cr" onclick="openPage('Initial.jsp')" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
function openPage(pageURL) {
window.location = pageURL;
}
</script>
<%
String x = request.getParameter("conf");
if (x != null && x.equals("confirm")) {
//response.sendRedirect("/Initial");
RequestDispatcher dispatcher = request.getRequestDispatcher("/Initial");
dispatcher.forward(request, response);
}
%>
</body>
</html>
Please help me . Any help would be greatly appreciated. Thanking You.
you have to write
<form action=/your_servlet_page_name>
And you have to use
<input type="submit" value="confirm" name="conf"/>
And also you have to map your servlet page into web.xml file like
<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/CheckLogin</url-pattern>
</servlet-mapping>
<form action = "servlet-name" method = "method in the servlet">
<input type ="submit" value = "val">
</form>
This is a simple way to do this. If you are using the latest jre i think 7 and up, you don't need to declare the servlet in your web.xml file. the #WebServlet("/servlet-url") will do the trick.
if you want to use type="button" instead of type="submit". you can use javascript function on the click on the button. Like
<script>
function doSubmit(){
var actionURL ="MENTION URL YOU WANT TO REDIRECT";
// perform your operations
myForm.submit(actionURL); OR
myForm.submit();
}
</script>
<form name="myForm">
<input type="button" name="conf" value="conf" obclick="doSubmit();">
</form>
hope it will help you.
try by changing the script only
<script type="text/javascript">
function openPage(pageURL)
{
window.location.href = pageURL;
}
</script>
function openPage(pageURL) {
window.location = pageURL;
}
In above code snippet, pageURL has to be an absolute URL which in case of dealing with servlets may vary.So instead of this below can be used
location.href = (location.href).substr(0, (location.href).lastIndexOf('xyz.jsp'))+"/abc";
Here 'abc' is the servlet to which we have to redirect the 'xyz.jsp' .This can even work if there are many buttons.The respective functions could be written to redirect to respective servlets.
Also it works in case of input type is "button" or "submit".
I dont get exactly what you are trying to do,but redirect is working with:
response.sendRedirect(request.getContextPath());
or
response.sendRedirect(String url);

SpringMVC - Send nested form in jsp

I have a form (addPassForm) which is nested in another form (addStoreFrom) in the JSP page. How can I send the addPassForm form to the controller?
When I try to send the addPassForm form, addStoreFrom form sends instead.
<s:url value="/addStore" var="urlAddStore"/>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<table border="1">
<tbody>
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</tbody>
</table>
</form:form>
It is just because nested forms in not a valid HTML pattern. The browser simply ignore the inner <form></form> tags and sees only one form. Reference : Is it valid to have a html form inside another html form?
It is not a JSP problem (nor a Java one !), but only a incorrect HTML problem. You must use successive forms instead of nested forms (or user javascript as other suggested)
Example with successive forms :
<s:url value="/addStore" var="urlAddStore"/>
<table border="1">
<tbody>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</form:form>
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
</tbody>
</table>
Explicitly call submit() function in javascript.Add a function and bind it to onclick of add button.<input type="button" value="Add" onclick="submitAddPassForm()"/>.In javasrcipt simply use:
function submitAddPassForm(){
$('#addPassForm').submit();
}
You can just make a ajax post call instead of a form submit

How to send values to a servlet from a jsp which has directly retrieved values from another jsp

my motive is data flow from modify.jsp to modifysummary.jsp then to servlet to update details but i dont want to use scriptlets in my jsp page.
modify.jsp
<form action="modifysummary.jsp">
<table border="1">
<tr>
<td><label>FirstName</label></td>
<td><input type="text" name="firstname" value="<%=bean.getFirstname() %>"></td>
</tr>
<tr>
<td><label>Surname</label></td>
<td><input type="text" name="surname" value="<%=bean.getSurname() %>" ></td>
</tr>
</form>
modifysummary.jsp
<table>
<tr >
<th>Employee Details</th>
</tr>
<tr>
<td>First Name</td>
<td><%=request.getParameter("firstname") %></td>
</tr>
<tr>
<td>Surname</td>
<td><%=request.getParameter("surname") %></td>
</tr>
<tr>
<td><input type="button" value="BACK" onclick="javascript:history.go(-1)"></td>
<td><input type="submit" value="Modify" name="Modify"></td>
</tr>
</table>
i have written this code inside a form,on clicking the modify button the data should go to servlet in a bean but how to do that.
i think this is not possible without using scriptlets in your modifysummary.jsp . you must set your username and password into request attributes
using request.setattribute();
and get them in servlet
using request.getattribute();
why you don't want to use scriptlets.
and if you want to use requestdispatcher you have to write all that in scriptlets

Categories