• Post Reply Bookmark Topic Watch Topic
  • New Topic

Attribute value is null

 
Thirumani Selvam Muthuswamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not getting Attribute values as null,

I am using scriplet for assigning the attribute in jsp as below
<% request.setAttribute("testParam","check");%>

if i try to get the same attribute in servlet as below

request.getAttribute("testParam"); i am getting null value

This is the first attribute, i am using in this application
 
Paul Clapham
Sheriff
Posts: 21882
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what exactly is the relationship between that JSP and the servlet which you referred to?
 
Tapas Chand
Ranch Hand
Posts: 602
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to learn more about http requests.
Whenever we request a Servlet, that is a new request, so it does not remember anything about the values set.
If you want to pass some value from JSP to Servlet you can use HTML hidden fields or Session if you want that value to be available for that session.

And DO NOT use scriplet in JSP. That is obsolete since some 13 years.
Use EL.
 
Thirumani Selvam Muthuswamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:And what exactly is the relationship between that JSP and the servlet which you referred to?


My Jsp code is

<%@ 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>Sample Web Application</title>
</head>
<body>
<form action="MyServlet" method="post" >
<% request.setAttribute("requestParam", "Check"); %>
<table >
<tr>
<td></td>
<td > Please Login here</td>
</tr>
<tbody>
<tr >
<td>UserName :</td>
<td><input type= 'text' id="userName"/></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" id="Pass"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value ="Submit"/></td>
</tr>
</tbody>
</table>
<table >

</table>
</form>
</body>
</html>


My Servlet code is

public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public MyServlet() {
// TODO Auto-generated constructor stub
}

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

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
String param = (String) request.getAttribute("requestParam");
System.out.println("Username : "+request.getParameter("userName"));
System.out.println("Password : "+request.getParameter("Pass"));
//String strParam =(String) request.getAttribute("requestParam");
System.out.println("Output From Servlet");
//System.out.println("Received Attribute: "+strParam);
if(param.equalsIgnoreCase("Check"))
{
System.out.println("Param is correct");
PrintWriter prw = response.getWriter();
}
}

}

content in web.xml is

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SampleWebApplication</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>MyServlet</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.sample.Servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyServlet</url-pattern>
</servlet-mapping>
</web-app>

In the above code, i am using Myservlet in forms's action. it is redirecting to "MyServlet" but i am not getting the attribute values. sameway i am not getting parameters username and pass

please let me know , is anything wrong in this?
 
Thirumani Selvam Muthuswamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:You need to learn more about http requests.
Whenever we request a Servlet, that is a new request, so it does not remember anything about the values set.
If you want to pass some value from JSP to Servlet you can use HTML hidden fields or Session if you want that value to be available for that session.

And DO NOT use scriplet in JSP. That is obsolete since some 13 years.
Use EL.


yes, whenever we request it is new request. just i have one jsp, servlet. if i submit the jsp, it is re-directing to servlet, but i am getting null value for all attributes and parameters. i have pasted code
 
Tapas Chand
Ranch Hand
Posts: 602
9
BSD Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you are posting code, UseCodeTags <- this is a link. Go through this link. It will make your code more readable.

As far as your problem is concerned, let us break that into 2 parts.
1. Attribute values are null in Servlet.
2. Parameter values are null in Servlet.

1. Attribute is not the way you can pass a value from JSP to Servlet as I have mentioned in my previous post. You can take help of hidden HTML element.
2. Parameters are identified by "name" not "id" in a Servlet.

Let us know if you understand the problem now and solve the problem.
 
Thirumani Selvam Muthuswamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Whenever you are posting code, UseCodeTags <- this is a link. Go through this link. It will make your code more readable.

As far as your problem is concerned, let us break that into 2 parts.
1. Attribute values are null in Servlet.
2. Parameter values are null in Servlet.

1. Attribute is not the way you can pass a value from JSP to Servlet as I have mentioned in my previous post. You can take help of hidden HTML element.
2. Parameters are identified by "name" not "id" in a Servlet.

Let us know if you understand the problem now and solve the problem.


Thanks it is working now
 
Tapas Chand
Ranch Hand
Posts: 602
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good work :thumbup:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!