I have a question.
I have 3 jsp page. The first is a menu with 2 button. When I click the first button I want to open the second jsp page. When I click the second button I want to open the third jsp page.
Can you help me? I must use a servlet(it's not a problem, i know it)?
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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 name="TrainerMenu" action="TrainerMenu" method="get">
<h1>Benvenuto in LESSON! Scegli l'operazione da effettuare:</h1>
<input type="button" value="Creazione Nuovo Corso" name="CreateCourse" />
<input type="button" value="Gestione Autorizzazioni"
name="AuthorizationManager" />
</form>
</body>
</html>
The simplest way you can do this is to use java script.
For example, <input type="button" value="load" onclick="window.location='userpage.jsp'" >
If you wanna do with a button click and not the other way. You can do it by adding location.href
to your button. Here is how I'm using
<button class="btn btn-lg btn-primary" id="submit" onclick="location.href ='/dashboard'" >Go To Dashboard</button>
The button above uses bootstrap classes for styling. Without styling, the simplest code would be
<button onclick="location.href ='/dashboard'" >Go To Dashboard</button>
The /dashboard
is my JSP page, If you are using extension too then used /dashboard.jsp
You could make those submit buttons and inside the servlet your are submitting the form to you could test the name of the button which was pressed and render the corresponding jsp page.
<input type="submit" value="Creazione Nuovo Corso" name="CreateCourse" />
<input type="submit" value="Gestione Autorizzazioni" name="AuthorizationManager" />
Inside the TrainerMenu
servlet if request.getParameter("CreateCourse")
is not empty then the first button was clicked and you could render the corresponding jsp.
Just use two forms.
In the first form action attribute will have name of the second jdp page and your 1st button. In the second form there will be 2nd button with action attribute thats giving the name of your 3rd jsp page.
It will be like this :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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 name="main1" method="get" action="2nd.jsp">
<input type="submit" name="ter" value="LOGOUT" >
</form>
<DIV ALIGN="left"><form name="main0" action="3rd.jsp" method="get">
<input type="submit" value="FEEDBACK">
</form></DIV>
</body>
</html>
If all you are looking for is navigation to page 2 and 3 from page one, replace the buttons with anchor elements as below:
<form name="TrainerMenu" action="TrainerMenu" method="get">
<h1>Benvenuto in LESSON! Scegli l'operazione da effettuare:</h1>
<a href="Page2.jsp" id="CreateCourse" >Creazione Nuovo Corso</a>
<a href="Page3.jsp" id="AuthorizationManager">Gestione Autorizzazioni</a>
<input type="button" value="" name="AuthorizationManager" />
</form>
If for some reason you need to use buttons, try this:
<form name="TrainerMenu" action="TrainerMenu" method="get">
<h1>Benvenuto in LESSON! Scegli l'operazione da effettuare:</h1>
<input type="button" value="Creazione Nuovo Corso" name="CreateCourse"
onclick="openPage('Page2.jsp')"/>
<input type="button" value="Gestione Autorizzazioni" name="AuthorizationManager"
onclick="openPage('Page3.jsp')" />
</form>
<script type="text/javascript">
function openPage(pageURL)
{
window.location.href = pageURL;
}
</script>
It works using ajax. The jsp then display in iframe returned by controller in response to request.
function openPage() {
jQuery.ajax({
type : 'POST',
data : jQuery(this).serialize(),
url : '<%=request.getContextPath()%>/post_action',
success : function(data, textStatus) {
jQuery('#iframeId').contents().find('body').append(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
}
});
}
Source: Stackoverflow.com