473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 10156
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

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

Similar topics

5
by: Haisoo Shin | last post by:
Hello.. I am working on a PHP/PEAR web page that shows statistics of how many people read a certain article during given period of time. I have, say, two tables called 'books' and 'logs'. The...
7
by: Kannan | last post by:
Hello, I have a situation which would essentially use a co-related subquery. I am trying to avoid using a co-related subquery due to its slow performanc and use a join statement instead. Here...
17
by: Pushkar Pradhan | last post by:
I want to time my matrix multiply code (in MFLOPS). I want to run the code 100,000 times or some other big number. This can be done 2 ways (for and while loops): timer1 = time(NULL); for(n = 0;...
2
by: Samuel | last post by:
Imagine you have the following code: try { ... } catch (ThreadAbortException eThread) { if (WorkStopped != null) WorkStopped(this, EventArgs.Empty) }
11
by: Homam | last post by:
The ASP.NET model is touted as a clean OO approach to developing web pages, yet sometimes the page lifecycle poses silly obstacles that forces you revert to the Olde ASP 3.0 Ways. Here's a rough...
0
by: cbkprasad | last post by:
Hi, I have a critical problem regarding, avoiding the user to login to wibsite using the same credentials at the same time.The concept is once the user is logged in to application the that...
6
by: howa | last post by:
Since it is part fo the standard, why I always heard that we should avoid iframe? any comments? thanks.
0
by: halex | last post by:
Hello, I am having deadlock problem when I have a lot of visitors on my website at the same time. I am using NetTiers templates to generate C# classes for accessing DB layer and problem is in my...
12
by: doublemaster007 | last post by:
class A; A *a = new A; A *b, *c; b= a; c= b; delete a;
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.