473,394 Members | 1,178 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,394 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 10176
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;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.