Get data from jsp to another - java

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") %>

Related

JasperException when using f: in jsp page

I'm trying t learn Spring MVC and my project is buid a jsp form input a product
I added all jar jstl lib, and here is my taglib
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<%#taglib uri="http://www.springframework.org/tags" prefix="s"%>
Here is my form appears bug
`<f:form action ="insertProduct.htm" method="GET" commandName="newProduct">
<table border ="1" cellpadding="2" cellspacing="2">
<tr>
<td>Product ID</td>
<td><f:input path="productId"/></td>
</tr>
<tr>
<td>Product Name</td>
<td><f:input path="productName"/></td>
</tr>
<tr>
<td>Price</td>
<td><f:input path="price"/></td>
</tr>
<tr>
<td>Description</td>
<td><f:input path="description"/><td>
</tr>
<tr>
<td>Status</td>
<td>
<f:select path="status">
<f:option value="true" label="Active"/>
<f:option value="false" label="Inactive"/>
</f:select>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Insert"/>
<input type="reset" value="Reset"/>
<input type="button" value="Back" onclick="history.go(-1)"/>
</td>
</tr>
</table>
</f:form>`
But i cant fix my bug. Please help me :(

Extend MasterPage in JSP

I have a source code father JSP like that:
<table id="1" >
<tr>
<td>Table1</td>
</tr>
</table>
<div id=".insertCodeJSPInHere"></div>
<table id="2">
<tr>
<td>Table2</td>
</tr>
</table>
I want when i extend father JSP in children JSP then children JSP like that
<table id="1" >
<tr>
<td>Table1</td>
</tr>
</table>
<div id=".insertCodeJSPInHere"><h1>YenDV</h1></div>
<table id="2">
<tr>
<td>Table2</td>
</tr>
</table>
I write in children JSP:
<div id=".insertCodeJSPInHere"><h1>YenDV</h1></div>
<%#include file="fatherJSP.jsp" %>
But it's not work. I don't understand? HELP!
Try
<jsp:include page="fatherJSP.jsp">
The include directive:
<%#include file="something.html" %>
Used for static imports like html pages

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

Linking HTML to JSP page

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

How to programmatically login to j_security_check

I have a JSP web application which uses j_security_check. Is it possible to login a specific user to j_security_check programmatically via a JSP page if I know the userid and password? I tried to pass the variables as URL parameters this way...
response.sendRedirect(www.mydomain.com/j_security_check?j_username=someUserName&j_password=somePassword )
...but it doesn't work. Is there any way to do it?
Edit:
here is my login page which works fine right now. I trimmed some of the code for security reasons.
<form name="signon" method="post" action="/j_security_check">
<table width="100%" cellpadding="4" cellspacing="0">
<tr>
<td valign="top" colspan="4">
<h2><%= UI.tr(null, "login_details") %>
</h2>
</td>
</tr>
<tr>
<td valign="top" width="150px">
<%= UI.tr(null, "login_id") %>
</td>
<td valign="top" width="150px">
<%= UI.tr(null, "login_pass") %>
</td>
<td valign="top" width="150px">
<%= UI.tr(null, "login_token_or_captcha") %>
</td>
<td width="100%"> </td>
</tr>
<tr>
<%
if (logins == null) {
%>
<td>
<input type="hidden" name="j_uri" value="/index.jsp">
<input type="text" id="username" name="j_username" size="16" style="width: 150px;" autocomplete="off" <%= username == null ? "" : "disabled value='" + username + "'" %> onblur="return checkCaptcha();">
</td>
<%
} else {
%>
<td>
<select name="j_username" style="width: 150px;">
<%
for (Login login : logins) {
%>
<option><%= login.getUsername() %>
</option>
<%
}
%>
</select>
</td>
<%
}
%>
<td><input type="password" name="j_password" size="16" style="width: 150px;"> </td>
<td><input type="text" id="mypw" name="mypw" size="16" autocomplete="off" style="width: 150px;"></td>
<td><input class="submit" type="submit" name="submit" value="<%= UI.tr(null, "login_submit") %>"></td>
</tr>
<tr>
<td valign="top" colspan="4">
<%-- <%
if("registry.nic.hn".equals(request.getServerName())) {
%>
<!-- GARTH - put whatever you want here for .HN -->
<% } else { %> --%>
<h2><%= UI.tr(null, "login_news") %>
</h2>
<div><%= HTMLFormat.addBreaks(SiteConf.getSiteConf().getNews()) %>
</div>
<%-- <% } %> --%>
</td>
</tr>
The following code works for me in the JSP file:
String url = "j_security_check?j_username=" + username + "&j_password=" + password;
String redirectUrl = response.encodeRedirectURL(url);
response.sendRedirect(redirectUrl);
In my views, login using j_security_check url by appending username and password seems to be a big security venerability.
Instead you can perform following steps:
create a separate JSP which has a form
this form POST on j_security_check url with username and password
you can dynamically include this JSP
this JSP can have onload (JS) function which can POST this form.
In this way it will be secure.
You stated you want to login programatically. I would avoid using j_security_check if this is your goal. j_security_check is used with Form-Based Authentication (Check out this Oracle resource on Securing Web Applications - Form Authentication is covered about half way down: http://docs.oracle.com/cd/E24329_01/web.1211/e24485/thin_client.htm).
I would suggest you take a look at that resource and dive into some of the alternatives, but another approach altogether is to use the Java Authentication and Authorization Service (JAAS) API (Here is a tutorial link: http://docs.oracle.com/javase/6/docs/technotes/guides/security/jaas/tutorials/GeneralAcnOnly.html) JAAS and a callback handler is the non-abstraction way of programmaticly addressing authentication (and authorization). Many robust frameworks are based on this JAVA API, so it might be worth taking a look if you are still struggling with this issue.
You can have the user enter the username and password on a jsp page and a servlet or use a facelets page and a bean as described here to login a user.

Categories