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>
Related
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 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");
I was given a HTML page I changed it's extension .jsp. I want to make dynamic web application using struts. My Html page code is like this..
<div id="content">
<div class="subpage-border-top">
<div class="subpage-border-bottom">
<div class="subpage-border-mid">
<div class="loginpage-bottom">
<h2>Login</h2>
<div class="loginpage-mid">
<span>Enter your email address and password to login</span>
<ul>
<li><label>Email</label>
<div class="right_input">
<input type="text" value=""
onblur="if(this.value=='')this.value=this.defaultValue;"
onfocus="if(this.value==this.defaultValue)this.value='';"
class="o-que email-text" />
<!-- <strong> dshmbf,sdmhfdi</strong>-->
</div></li>
<li><label>Password</label>
<div class="right_input">
<input type="password" value=""
onblur="if(this.value=='')this.value=this.defaultValue;"
onfocus="if(this.value==this.defaultValue)this.value='';"
class="o-que email-text" />
</div> <a href="#" class="forget-password">(Forget Your
Password)</a></li>
<li>
<div class="remember">
<input type="checkbox" name="" />Remember Me
</div>
<div class="login-button">
<div>
<input type="submit" value="Login" />
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
So it's layout looks like this..
And according to a tutorial I want to use struts tags..
<s:form action="User_login">
<s:textfield label="User Name" key="userName"/>
<s:textfield label="Password" key="password"/>
<s:submit/>
</s:form>
But what should I replace with these struts tags,so my struts can work and my layout(css,java script) remain intact..Please help.
Your CSS will break because your HTML will change.
Struts2 uses Themes to generate the HTML for each tag; the default theme is XHTML.
Just use SIMPLE theme, that will generate ONLY the tag without any side data (<label> for example).
It can be applied to a single tag, to a form, or globally at application level.
Then try
<s:form action="User_login" theme="simple">`
; if you are satisfied with the result, set it globally in struts.xml with
<constant name="struts.ui.theme" value="simple" />
I am new to JSTL, could someone please tell me how to convert below JSP and HTML code to full JSTL with no scriptlet in the page?
I'd also be grateful for suggestions about some good resources to learn JSTL and advanced JSP concepts such as JSF and spring with CRUD example.
This is curd example taken from http://javaknowledge.info/?p=478. I took this example because I thought it is 100% JSTL implementation but I was wrong.
My IDE is NetBeans.
<form method="POST" action='UserController' name="frmAddUser">
<% String action = request.getParameter("action");
System.out.println(action);
%>
<% if (action.equalsIgnoreCase("edit")) {%>
User Name : <input type="text" name="uname"
value="<c:out value="${user.uname}" />" readonly="readonly"/> (You Can't Change this)<br />
<%} else {%>
User Name : <input type="text" name="uname"
value="<c:out value="${user.uname}" />" /> <br />
<%}%>
Password : <input
type="password" name="pass"
value="<c:out value="${user.password}" />" /> <br />
Email : <input
type="text" name="email"
value="<c:out value="${user.email}" />" /> <br />
<% if (action.equalsIgnoreCase("edit")) {%>
Registration : <input
type="text" name="dob"
value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registeredon}" />" readonly="readonly"/>(You Can't Change this) <br />
<%} else {%>
Registration : <input
type="text" name="dob"
value="<fmt:formatDate pattern="yyyy/MM/dd" value="${user.registeredon}" />" />(yyyy/MM/dd) <br />
<%}%>
<input type="submit" value="Submit" />
</form>
There are only two types of scriptles:
print the current action into system's out - there is no JSTL alternative
if-else statement can be rewritten using
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:choose>
<c:when test='${ fn:toLowerCase(param.action) eq "edit" }'>
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
Years ago i learnd alot from this videos:
https://www.youtube.com/user/3n3xus/videos
Too bad, its in German and eclipse.
You learn
how to write your own JSTL-Framework.
how to write and navigate in jsf
work with richfaces.
how to work with facelets.
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".