469,366 Members | 2,243 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

how to avoid null on a jsp page

hi,
Please have a look at the following jsp page code , when this page gets loaded "null" is being printed. Please suggest me where i am doing wrong. Servlet code is also added for more details.

Page: javatest.jsp
Expand|Select|Wrap|Line Numbers
  1. <%@page contentType="text/html"%>
  2. <%@page pageEncoding="UTF-8"%>
  3.  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  5.    "http://www.w3.org/TR/html4/loose.dtd">
  6.  
  7. <html>
  8.     <head>
  9.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  10.         <title>JSP Page</title>
  11.     </head>
  12.     <body>
  13.  
  14.     <h1>JSP Page</h1>
  15.  
  16.     <form name="contact"action="testJavaServlet" method="post" onSubmit="return checkMe();">
  17.                                             <FIELDSET>
  18.                                             <legend>Post Your Comments/Suggetions/Message </legend> 
  19.                                             <p>Message Type : <select  name="messagetype" class="selectbox">
  20.                                                 <option value="Nil">-----------Select Msg Type-----------</option>
  21.                                                 <option value="problem">If any PC related problems</option>
  22.                                                 <option value="suggetion">Suggetion to improve our Web Site</option>
  23.                                                 <option value="requirement">New requirements on our Web Site</option>
  24.                                                 <option value="compliments">Your compliments</option>
  25.                                             </select>
  26.                                             </p>                            
  27.                                             <p>Particulars : <input  name="visitor" class="textbox" /></p>
  28.                                             <p>Message :<textarea  name="message" rows="5" cols="10" class="textarea"></textarea></p>
  29.                                             <p><input type ="submit" name="submit" value="Submit" class="button" ></p>
  30.                                             </fieldset>
  31.                                             <%
  32.                                                String msg=" ";
  33.                                                 try{
  34.                                                     msg=request.getAttribute("msg").toString();
  35.                                                     if(msg==null) {
  36.                                                         msg="";
  37.                                                     }
  38.                                                     out.print(msg);
  39.                                                 }catch(Exception e){
  40.                                                     out.print(e.getMessage());
  41.                                                 }
  42.  
  43.  
  44.                                                 %>
  45.                                             </form>
  46.  
  47.     </body>
  48. </html>
Servlet Code: testJavaServlet.java
Expand|Select|Wrap|Line Numbers
  1. package webservlets;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7. import webservlets.UserBean;
  8. import webservlets.UserDAO;
  9.  
  10. public class testJavaServlet extends HttpServlet {
  11.     UserBean UserBean = new UserBean();
  12.     UserBean u = new UserBean();
  13.  
  14.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  15.     throws ServletException, IOException {
  16.         UserBean.setMessageType(request.getParameter("messagetype"));
  17.         UserBean.setVisitor(request.getParameter("visitor"));
  18.         UserBean.setMessage(request.getParameter("message"));
  19.         UserBean.setIP(request.getRemoteAddr());
  20.  
  21.         UserDAO UserDAO = new UserDAO(UserBean);
  22.         boolean result = UserDAO.insertData(UserBean);
  23.  
  24.         if(result){
  25.             RequestDispatcher rd = 
  26.  
  27. getServletContext().getRequestDispatcher("/javatest.jsp");
  28.             request.setAttribute("msg","Thank You!");
  29.             rd.forward(request,response);
  30.  
  31.  
  32.         } else{
  33.             RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
  34.             rd.forward(request,response);
  35.               request.setAttribute("msg","Sorry we could not collect your request!");
  36.         }
  37.     }
  38.  
  39.  
  40. }
With Thanks and Regards
Naveen
Nov 4 '08 #1
16 9374
r035198x
13,262 8TB
In your servlet, you should call request.setAttribute before forwarding the request or calling the requestDispatcher.
Nov 4 '08 #2
In your servlet, you should call request.setAttribute before forwarding the request or calling the requestDispatcher.
Hi,
Thank you, I have tried this but the result remains same. Problem still exists. We may require something else to resolve this issue
Nov 4 '08 #3
r035198x
13,262 8TB
Is that JSP always called from that servlet? If not then that attribute will not be available i the JSP since it's coming from the servlet.
Nov 4 '08 #4
Is that JSP always called from that servlet? If not then that attribute will not be available i the JSP since it's coming from the servlet.
Yes JSP is always called from same servlet. While loading the JSP page itself null is being displayed. Is there any way to trap the null value to avoid this issue. I have tried but not able to find the solution.
Nov 4 '08 #5
r035198x
13,262 8TB
Are other JSPs displaying properly?
Nov 4 '08 #6
itsraghz
127 100+
First of all, can you check and ensure that the request hits the servlet correctly? You can do so by putting a debug information (a SOP or log).
Nov 4 '08 #7
First of all, can you check and ensure that the request hits the servlet correctly? You can do so by putting a debug information (a SOP or log).
I have checked everything. Request hitting the servlet correctly but before sending the request to the servlet null is being printed. How to avoid this?
Nov 6 '08 #8
itsraghz
127 100+
I have checked everything. Request hitting the servlet correctly but before sending the request to the servlet null is being printed. How to avoid this?
Yes of course. That's because your request has not yet hit the servlet and thats where you are actually setting the "msg" attribute with some value.

In such case, the server assumes that you are trying to retrieve an object with the name "msg" and it will NOT find any. In such case the obvious return value is "null" as one is not present.

You can have a conditional printing of "msg" value for its NULL value!
Nov 6 '08 #9
r035198x
13,262 8TB
I have checked everything. Request hitting the servlet correctly but before sending the request to the servlet null is being printed. How to avoid this?
I thought you said that the JSP is always called from the servlet?
Nov 6 '08 #10
I thought you said that the JSP is always called from the servlet?
Sorry to say that it is a mistake from my side, first the jsp page gets loaded thereafter on request servlet will be called
Nov 6 '08 #11
Yes of course. That's because your request has not yet hit the servlet and thats where you are actually setting the "msg" attribute with some value.

In such case, the server assumes that you are trying to retrieve an object with the name "msg" and it will NOT find any. In such case the obvious return value is "null" as one is not present.

You can have a conditional printing of "msg" value for its NULL value!
Where i have suppose to do it? As i have already tried on jsp page for catching msg= NULL but it is not happening. If i need to do it at servlet end then i will do it and get back to you
thank you
Nov 6 '08 #12
r035198x
13,262 8TB
Sorry to say that it is a mistake from my side, first the jsp page gets loaded thereafter on request servlet will be called
Well then as you have seen, when you load it first then there is no msg attribute because it's coming from a servlet which has not been called.
Nov 6 '08 #13
itsraghz
127 100+
Where i have suppose to do it? As i have already tried on jsp page for catching msg= NULL but it is not happening. If i need to do it at servlet end then i will do it and get back to you
thank you
No need to do anything in a servlet apart from just setting the value and forwarding it.

Looking at your code in JSP page,

Expand|Select|Wrap|Line Numbers
  1. <%
  2. String msg=" ";
  3. try{
  4. msg=request.getAttribute("msg").toString();
  5. if(msg==null) {
  6. msg="";
  7. }
  8. out.print(msg);
  9. }catch(Exception e){
  10. out.print(e.getMessage());
  11. }
  12. %>
Can you put a debug statement (SOP or log) before the if statement and check what exactly you get from request scope? It might tell you the *real value* you get!
Nov 6 '08 #14
No need to do anything in a servlet apart from just setting the value and forwarding it.

Looking at your code in JSP page,

Expand|Select|Wrap|Line Numbers
  1. <%
  2. String msg=" ";
  3. try{
  4. msg=request.getAttribute("msg").toString();
  5. if(msg==null) {
  6. msg="";
  7. }
  8. out.print(msg);
  9. }catch(Exception e){
  10. out.print(e.getMessage());
  11. }
  12. %>
Can you put a debug statement (SOP or log) before the if statement and check what exactly you get from request scope? It might tell you the *real value* you get!
I have tried but no value is coming out in request scope , that may be the reason it is printing null.
Nov 11 '08 #15
I have tried but no value is coming out in request scope , that may be the reason it is printing null.
This issue still alive please give a solution to resolve this.
Nov 15 '08 #16
r035198x
13,262 8TB
When you load that JSP for the first time there will be nothing in that attribute because it never went to the servlet where the value is supposed to be set.
Nov 17 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Haisoo Shin | last post: by
17 posts views Thread by Pushkar Pradhan | last post: by
2 posts views Thread by Samuel | last post: by
11 posts views Thread by Homam | last post: by
6 posts views Thread by howa | last post: by
12 posts views Thread by doublemaster007 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.