Site icon NgDeveloper

Passing clicked value from jsp to jsp/servlet

Passing value from jsp to jsp/servlet:

We can pass the value from jsp to servlet using,

request.getParameter("text"); //servlet code to get the jsp value

where text is the name of the input fields (text field).

Passing clicked value from jsp to jsp/servlet:

We have to pass the clicked value as get parameter in the href link like below,

<a href="/Myservlet?input=javadomain">

MyServlet is the name of the servlet.

Servlet code:

request.getParameter("input");

So javadomain will be passed from jsp to servlet.

In the below sample program javadomain (small letters) will be clicked, then the same value passed from jsp to servlet and there javadomain(small letters) are converted to JAVADOMAIN (UPPER CASE) then the new upper case values are passed from servlet to jsp.

Program:

main.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Small Letters</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<a href="/Blog/ToCapLetters?a=javadomain">javadomain</a>
</body>
</html>

 final.jsp:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Capital Letters</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="GENERATOR" content="Rational Application Developer">
</head>
<body>
<%
HttpSession session1 = request.getSession();
String capitalValue = session1.getAttribute("clicked_value").toString();
out.println(capitalValue);
 %>
</body>
</html>

 ToCapLetters.java: (servlet file)

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ToCapLetters
 */
@WebServlet("/ToCapLetters")
public class ToCapLetters extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToCapLetters() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String value = request.getParameter("a");
		String value_capital = value.toUpperCase();
		HttpSession session = request.getSession();
		session.setAttribute("clicked_value", value_capital);
		RequestDispatcher rd = request.getRequestDispatcher("./final.jsp");
		rd.forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

 

 Output:

 

 

 

 

 

Thanks for reading this post……..!!!

Exit mobile version