I am new to JSP, and have a question as below.
In servlet1, I can use request.getParameter() to get the value to SELECT var1.
String ciStr = request.getParameter("var1")
But is there any way I can get the SELECT var1 from servlet2?
Thanks.
JSP code
<form action="Servlet1" method="post" enctype="multipart/form-data">
Confidence Interval
<SELECT name="var1" >
<OPTION value="ci99">99%</OPTION>
<OPTION value="ci95">95%</OPTION>
<OPTION value="ci90">90%</OPTION>
</SELECT> <br>
<textarea name="textArea1" style="width:500px;height:150px;"></textarea> <br>
<input type="submit" value="Submit" size="20" />
</form>
<form action="Servlet2" method="post" enctype="multipart/form-data">
Confidence Interval
<SELECT name="var2" >
<OPTION value="ci99">99%</OPTION>
<OPTION value="ci95">95%</OPTION>
<OPTION value="ci90">90%</OPTION>
</SELECT> <br>
<textarea name=textArea2" style="width:500px;height:150px;"></textarea> <br>
<input type="submit" value="Submit" size="20" />
</form>
Yes, You can use HttpSession object to set the value over server and then get these value any where on server.
HttpSession hs=request.getSession();
and you can set the value with a unique name .
hs.setAttribute("name","value");
and get any where on server by use the name.
String var=(String)hs.getAttribute("name");
Related
I'm a beginner in Spring and I'm trying to fix some small problem in my application. I have a form to edit the user profile. In this form, I have one dropdown list where admin user can select user role for the user that is being edited. When I'm going to edit user page then all data in form are populated from the database. Let's say I have two inputs in the form: input with lastName and select with user role. The select element contains all the roles that exist in the database but the selected role is matched to the role that the user actually have assigned.
This is part of my form - input lastName
<spring:bind path="lastName">
<div class="form-group">
<label for="lastName">Last name</label>
<form:input path="lastName" type="text" class="form-control ${status.error ? 'border border-danger' : ''}" id="lastName" />
<form:errors path="lastName" cssStyle="color: #ff0000;"/>
</div>
</spring:bind>
and this is a select element:
<form:select path="roles" id="role" multiple="false">
<form:options items="${allRoles}" itemValue="id" itemLabel="role"/>
</form:select>
Now, when I want to edit lastName eg. leave the blank field, then spring validates this field and throws an error. The problem is that role that was previously selected in select element is now unselected.
This is page HTML before validation:
input:
<div class="form-group ">
<label for="email" class="active">Email</label>
<input id="email" name="email" type="email" class="form-control" value="ola#test.com">
</div>
select:
<select id="role" name="roles"">
<option value="1">ADMIN</option>
<option value="2">USER</option>
<option value="3">STUDENT</option>
<option value="4" selected="selected">LECTURER</option>
</select>
and once lastName field is cleared and form submited:
input:
<div class="form-group">
<label for="lastName" class="">Last name</label>
<input id="lastName" name="lastName" type="text" class="form-control border border-danger" value="">
<span id="lastName.errors" style="color: #ff0000;">This field is required.</span>
</div>
select:
<div class="form-group">
<label for="role">Role Id</label>
<select id="role" name="roles">
<option value="1">ADMIN</option>
<option value="2">USER</option>
<option value="3">STUDENT</option>
<option value="4">LECTURER</option>
</select>
</div>
As you can see selected= "selected" attribute has disappeared from option 4. How to prevent this? Btw. I'm aware of this Spring MVC selected value in form:selected after form validation error
but it seems that this isn't work in my case.
I found some tip in archived page here
Now selected attribute is not removed from the option tag. I'm not sure if this is good way to solve this, but it works for me..
Modified select:
<form:form method="post" modelAttribute="editForm">
<c:forEach items="${editForm.roles}" var="role">
<c:set var="userRoleId" value="${role.id}" scope="request"/>
</c:forEach>
<form:select path="roles" id="role" multiple="false">
<c:forEach var="tempRole" items="${allRoles}">
<option value="${tempRole.id}" <c:if test="${tempRole.id == userRoleId}">selected="selected"</c:if> value="${tempRole.id}">${tempRole.role}</option>
</c:forEach>
</form:select>
</form:form>
I have registration form jsp page that calls a servlet to add data to mysql database. In the same page I have 2 dropdowns, First one is country which is hard-coded.
According to the selected value, second combobox should populate with the appropriate city_name values before submit the form.
But I can't use php here.
table1-->user(fname,lname,country,city_id)
table2-->city(city_id,city_name,country)
HTML,
<form role="form" name="form1" action="Registration" method="post" class="signup-form">
<div class="form-group">
<select
onchange="document.getElementById('country').value=this.options[this.selectedIndex].text; document.getElementById('idValue').value=this.options[this.selectedIndex].value;">
<option></option>
<option value="sl">Sri Lanka</option>
<option value="jp">Japan</option>
<option value="en">England</option>
<option value="kr">Korea</option>
<option value="us">USA</option>
</select>
<input name="country" placeholder="Select Your Country" id="country" onfocus="this.select()" type="text" required>
</div>
<div class="col-md-4">
<select id="city" name="city" class="form-control">
<option>--Select--</option>
</select>
<label id="city1" class="input-group-error form-error"></label>
</div>
</form>
I have a <div> tag:
<div id="content">
<jsp:include page="some.jsp"/>
</div>
Also I have a few buttons:
<input type="submit" name="btn1" value="Page1">
<input type="submit" name="btn2" value="Page2">
I need when I click btn1 some.jsp changes to page1.jsp and to page2.jsp when click btn2.
Use dynamic include,
<jsp:include page="<%= myVariable %>" flush="true" />
Pd: Take a look a flush http://www.coderanch.com/t/484149/JSP/java/flush-true-jsp-include-tag.
1- Instead of using static include, you can use dynamic include, then you can do something like this:
<jsp:include page="${somePage}" flush="true" />
2- Use javascript to change the action of the form depending on the button you click:
<input type="submit" value="Page1" name="btn1"
onclick="document.forms[0].action = 'somePage.jsp'; return true;" />
Im using this solution : My form is :
<FORM>
<select name="choix">
<option value="choix 1">choix 1</option>
<option value="choix 2">choix 2</option>
<option value="choix 3">choix 3</option>
</select>
<input type="submit" />
</FORM>
and im using this in the same jsp page to include a page as what i select in that form:
<%
Ch = request.getParameter("choix");
%>
<div id="inculde_page">
<jsp:include page="<%= "layouts/" + Ch + ".jsp" %>" />
</div>
I'm working on an application that uses struts, and I don't fully understand struts yet;
If I have the following in a jsp page:
<form action="/accountProcess" class="" id="" method="post" enctype="multipart/form-data" autocomplete="off">
<select name="user_status_filter">
<option value="no_filter">no filter</option>
<option value="inactive">Inactive</option>
<option value="active">Active</option>
</select>
<input type="submit" value="submit"/>
</form>
and inside my struts-confix.xml:
<action-mappings>
<action path="/accountProcess"
name="UserForm"
type="x.y.UserAction"
scope="request"
parameter="dispatch"
input="pages.account"
validate="false"
roles="user">
</action>
</action-mappings>
and inside my UserAction class:
public ActionForward getUsers(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
throws Exception {
String requestValue = RequestUtils.getStringParameter(request, "user_status_filter");
return mapping.findForward("adminView");
}
How do I get my form to call the getUsers method in my UserAction class? Do I have to pass some hidden parameter in my form, and if so, how do I do this? In my form, is my action attribute set correctly? And would the form method be post or get?
found the solution, I added a hidden field for the html where the value is the name of the method
<form action="/accountProcess" class="" id="" method="post" enctype="multipart/form-data" autocomplete="off">
<html:hidden property="dispatch" value="getUsers" />
<select name="user_status_filter">
<option value="no_filter">no filter</option>
<option value="inactive">Inactive</option>
<option value="active">Active</option>
</select>
<input type="submit" value="submit"/>
</form>
Trying to submit checked selectbox value(on at a time) along with textbox value to a servlet;here is my code(jsp) :
<script type="text/javascript">
function search(){
document.f2.action="/InfoUser/SearchBox";
document.f2.submit();
}
</script>
<form name="f2">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> ----------- </option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search" onclick="search()">
</div>
</form>
and in the servlet : I have written
String[] select = request.getParameterValues("select");
String search = request.getParameter("search");
Unable to fetch the value in servlet,am I missing some javascript/jquery script to get the value in servlet & how I can use it with the help of JSTL.Any rectification if I'm going wrong .....feel free to comment,will be welcome.
Try to change your form:
<form action="/InfoUser/SearchBox">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> -----------</option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search">
</div>
</form>
Or:
<script type="text/javascript">
function search(){
document.f2.action="/InfoUser/SearchBox";
document.f2.submit();
}
</script>
<form name="f2">
<div align="right">
<select id="select" name="select" style="color:#2D7EE7">
<option> ----------- </option>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
</select>
<input type="text" name="search" style="color:#2D7EE7">
<input type="submit" value="Search" onclick="search()">
</div>
</form>
You are missing the name of the form and in your javascript you are trying to set the action on non-existing form.
In servlet you have to do:
String select = request.getParameter("select");
String search = request.getParameter("search");
You won't be able to use: String[] select = request.getParameterValues("select"); unless your select element will have multiple="true".