Action tags are one of the factors in JSP pages, along with scripts, comments, and directives. 

In JSP, action tags are implemented to perform some precise tasks. 

Here are some most used action tags :

 

<jsp:forward> : to forward the request and response to other resources.

<jsp:forward page = "URL of another static, JSP, OR Servlet page" />

<jsp:include> : to include another resource.

<jsp:include page = "Page URL"  flush = "Boolean Value" />

<jsp:param> : to set the parameter for (forward or include) value.

<jsp: param name = "param_name" value = "value_of_parameter" />

<jsp:usebean> : to create or locate bean objects.

<jsp: useBean id="unique_name_to_identify_bean" class="package_name.class_name" />

and so on. 

 

Here, let us take a peek at some examples of these action tags. 

 

<Forward Action Tag Example 1>

 

forwardForm1.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>
	<h1>Forward Example</h1>

	<form method=post action="forwardFrom1.jsp">
	ID : <input type="text" name="id"><p>
	Password : <INPUT TYPE="password" NAME="password"><p>
			   <input type="submit" value="Submit">
	</form>

	</body>
</html>

forwardFrom.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
<body>
	<h2>Page forwarding: forwardFrom1.jsp</h2>

	<%
		request.setCharacterEncoding("euc-kr");
	%>

	forwardFrom1.jsp content
	<br> You will not see this on a browser.

	<%
		// request object
	request.setAttribute("name", "Meadow");
	%>

	<jsp:forward page="forwardTo1.jsp" />

</body>
</html>

forwardTo1.jsp

You will not see the forwarded data on the browser.

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>
	<h2>Page forwarding: forwardTo1.jsp</h2>

	<%
		String id = request.getParameter("id");
		String password = request.getParameter("password");
	%>

	<b><%=id%></b>'s<p>
	password is <b><%=password%></b>.

	<jsp:forward page="forwardTo2.jsp"/>  

	</body>
</html>

forwardTo2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

    
    ID : <%=request.getParameter("id")%> <br>
    password : <%=request.getParameter("password")%> <br>
    
    Print out the value shared by the request object.<br>
    <%=request.getAttribute("name")%>

<Forward Action Tag Example 2>

 

forwardForm2.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<head>
	<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
	<script>
	
	$(document).ready(function(){
		
		$(":submit").click(function(){
			
			if($("#name").val() == ""){
				alert("Insert your name.");
				$("#name").focus();
				return false;
			}
			
			if($("#a").is(":checked") == false &&
			   $("#b").is(":checked") == false &&	
			   $("#o").is(":checked") == false &&
			   $("#ab").is(":checked") == false){
				alert("Choose your bloodtype.");
				return false;
			}		
			
		});
		
	});	
	
	</script>
	</head>
	<body>

	<h1>Forwarding parameter value to next page</h1>

	<form method=post action="forwardFrom2.jsp">
	Name : <input type=text id="name" name="name"> <br><br>
	
	What is your bloodtype?<p> 
	
	<input type="radio" id="a" name="bloodType" value="a">A<br>
	<input type="radio" id="b" name="bloodType" value="b">B<br>
	<input type="radio" id="o" name="bloodType" value="o">O<br>
	<input type="radio" id="ab" name="bloodType" value="ab">AB<p>
	<input type="submit" value="Submit">
	</form>

	</body>
</html>

forwardFrom2.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>Page forwarding: forwardFrom2.jsp</h1>

	<%
		request.setCharacterEncoding("euc-kr");

		String bloodType = request.getParameter("bloodType") + ".jsp";
	
	%>

	<jsp:forward page="<%=bloodType%>"/>

a.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>Page forwarding(a.jsp)</h1>

	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>

	<b><%=name%></b>'s bloodtype is 
	<b><%=bloodType%></b> and did you know that <p>
	people with type B blood are more susceptible to gastroenteric cancers?

	</body>
</html>

b.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>Page forwarding(b.jsp)</h1>

	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>

	<b><%=name%></b>'s bloodtype is 
	<b><%=bloodType%></b> and did you know that <p>
	people of this blood type have a strong immune system and stable nervous system?

	</body>
</html>

ab.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>Page forwarding(ab.jsp)</h1>

	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>

	<b><%=name%></b>'s bloodtype is
	<b><%=bloodType%></b> and did you know that <p>
	type AB- is the rarest of all the blood types, with just 1% of the population having it?

	</body>
</html>

o.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h1>Page forwarding(o.jsp)</h1>

	<%
		String name = request.getParameter("name");
		String bloodType = request.getParameter("bloodType");
	%>

	<b><%=name%></b>'s bloodtype is
	<b><%=bloodType%></b>, and did you know that <p>
	people with the bloodtype O are less susceptible to malaria?

	</body>
</html>

<Forward Action Tag Example 3>

 

forwardForm3.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h2>Forward Example 3</h2>

	<form method="post" action="forwardFrom3.jsp">
	Name : <input type="text" name="name"><br><br>
	Your color : <br>
	<input type="radio" name="color" value="yellow">Yellow<br>
	<input type="radio" name="color" value="green">Green<br>
	<input type="radio" name="color" value="blue">Blue<br>
	<input type="radio" name="color" value="red">Red<p>
	<input type="submit" value="Confirm">
	</form>

	</body>
</html>

forwardFrom.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<html>
	<body>

	<h2>Page forwarding: forwardTagFrom2.jsp</h2>

<%
   request.setCharacterEncoding("euc-kr");

   String name = request.getParameter("name");
   String selectedColor = request.getParameter("color");
%>

<jsp:forward page="<%=selectedColor+\".jsp\"%>">
    <jsp:param name="selectedColor" value="<%=selectedColor%>"/>
	<jsp:param name="name" value="<%=name%>"/>
</jsp:forward>

blue.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<%
   String name = request.getParameter("name");
   String selectedColor = request.getParameter("selectedColor");
%>

<h2>Blue - <%=selectedColor+".jsp"%></h2>

<b><%=name%></b>'s color is "<%=selectedColor%>" and
it represents calmness and responsibility.<br><br>

<img src="<%=selectedColor+".jpg"%>" border="0" width="70" height="30">

green.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<%
   String name = request.getParameter("name");
   String selectedColor = request.getParameter("selectedColor");
%>

<h2>Green - <%=selectedColor+".jsp"%></h2>

<b><%=name%></b>'s color is "<%=selectedColor%>" and
it represents growth and renewal, being the color of spring and rebirth.<br><br>

<img src="<%=selectedColor+".jpg"%>" border="0" width="70" height="30">

red.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<%
   String name = request.getParameter("name");
   String selectedColor = request.getParameter("selectedColor");
%>

<h2>Red - <%=selectedColor+".jsp"%></h2>

<b><%=name%></b>'s color is "<%=selectedColor%>" and
it represents life, health, vigor, war, courage, anger, love and religious fervor.<br><br>

<img src="<%=selectedColor+".jpg"%>" border="0" width="70" height="30">

yellow.jsp

<%@ page contentType="text/html;charset=euc-kr"%>

<%
   String name = request.getParameter("name");
   String selectedColor = request.getParameter("selectedColor");
%>

<h2>Yellow- <%=selectedColor+".jsp"%></h2>

<b><%=name%></b>'s color is "<%=selectedColor%>" and
it symbolizes happiness, warmth and sunshine.<br><br>

<img src="<%=selectedColor+".jpg"%>" border="0" width="70" height="30">

By designating the name of the jpg files to the selectedColor, you can directly load them to the forward pages.

'Java > JSP' 카테고리의 다른 글

JSP) Action tags - include  (0) 2022.08.31
JSP) Handling Exceptions  (0) 2022.08.30
JSP) Scopes - Page / Request Scopes  (0) 2022.08.29
JSP) Session  (0) 2022.08.29
JSP) Cookies  (0) 2022.08.28

+ Recent posts