how to get value from request in jsp using jstl


how to get value from request in jsp using jstl

how to get value from request in jsp using jstl is an important concept for every java developer so let’s understand how to get value from request in jsp using jstl.

Example

  1. Login.jsp
  2. LoginHandler.java
  3. welcome.jsp

Login.jsp

<%@ 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>Login</title>
</head>
<body>
 <form action="${pageContext.request.contextPath }/LoginHandler">
 <table>
 <tr>
 <td>Username:</td>
 <td><input type="text" name="uname"></td>
 </tr>
 <tr>
 <td>Password:</td>
 <td><input type="password" name="pwd"></td>
 </tr>
 <tr>
 <td colspan="2"><input type="submit" value="Login"></td>
 </tr>
 </table>
 </form>
</body>
</html>

LoginHandler.java

package com.javagf.controllers;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 String uname = request.getParameter("uname");
 String pwd = request.getParameter("pwd");
 if (uname.equals("admin") && pwd.equals("admin")) {
 //set sername in request attribute
   request.setAttribute("uname", uname);
   request.getRequestDispatcher("./welcome.jsp").forward(request, response);
 }
 }

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 service(request, response);
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
 service(request, response);
 }

}

welcome.jsp

Let’s see how to get request value in jsp using jstl.

<%@ 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>
 <h1>${uname }</h1>
</body>
</html>

Output

admin






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229