473,499 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to link a jsp and a servlet?

41 New Member
Hi guys,

I am trying to link a jsp and servlets using beans, but how do I declare the id in the servlet. I can't seem to link them both. Help!!!!!


Thanks in advance.

Below is the jsp and servlet code:

Expand|Select|Wrap|Line Numbers
  1. <%@ page import='java.util.*‘ %>
  2.  
  3. <html>
  4. <head>
  5.  
  6. <LINK REL="stylesheet" TYPE="text/css" HREF="stylesheet1.css">
  7.  
  8. <head>
  9.  
  10. <body>
  11.  
  12. <hr></hr>
  13.  
  14. <center>
  15.  
  16. <h1>Weather Forecast</h1>
  17.  
  18.  
  19. <jsp:useBean id="today" scope="request" class="TestServlet" />
  20.  
  21.  
  22. <%= request.getOutputStream() %>
  23.  

Expand|Select|Wrap|Line Numbers
  1. import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4.  
  5. public class TestServlet extends HttpServlet {
  6.  
  7.     String  valString;
  8.  
  9.  
  10.             public void doPost(HttpServletRequest request, HttpServletResponse response) 
  11.             throws ServletException, IOException { 
  12.  
  13.             response.setContentType("text/html");
  14.             PrintWriter out = new PrintWriter(response.getOutputStream());
  15.  
  16.  
  17.              try { 
  18.  
  19.              FileReader dFile = new FileReader("DataFile.txt");
  20.  
  21.              BufferedReader dataFile = new BufferedReader(dFile);
  22.  
  23.  
  24.             valString = dataFile.readLine();
  25.  
  26.             if(valString != null)
  27.  
  28.             System.out.println(" Data is: " + valString);
  29.  
  30.  
  31.  
  32.         RequestDispatcher view =     request.getRequestDispatcher("weather.jsp");
  33.  
  34.         view.forward(request, response);
  35.  
  36.     dataFile.close();
  37.  
  38.  
  39.  
  40.             }
  41.  
  42.  
  43.         catch (FileNotFoundException e) {
  44.             System.err.println("FileStreamsTest: " + e);
  45.  
  46.  
  47.         }
  48.  
  49.  
  50.  
  51.             out.println("<html>");
  52.             out.println("<head><title>Temperature</title>");
  53.             out.println("</head><body>");
  54.             out.println("<b>The temperature today is: " +   valString + "</b><br>");
  55.             out.println("</body></html>");
  56.  
  57.             out.close();
  58.         }
  59.  
  60.  
  61.  
  62. }
  63.  
  64.  
Feb 14 '07 #1
0 1203

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

Similar topics

1
3875
by: Hai Tran | last post by:
Any help is appreciated. Installed Tomcat 4.1 and Mysql on a WinXP. I've manage to get Tomcat up and was able to view my first application ( myhome ) simple home page. Tomcat was installed under...
2
2455
by: Rose Girl | last post by:
Hi Need some help on javascript. I need to know how to call a servlet from a javascript function. web-inf (jboss) path for servlet is: com.purpleace.ripple.admin.contacts.servlet.AdminReport...
7
3572
by: bylum | last post by:
Servlet SQLException Communication link failure java howto i can't connect jsp and database(mysql). This is the exception: exception org.apache.jasper.JasperException:...
14
2384
by: ramadeviirrigireddy | last post by:
Hi All, I have the following code for form and servlet. when the form is submitted the servlet will print the values passed by the form. i'm not getting the servlet o/p when i submit the...
0
2630
by: krishna81m | last post by:
Could some one please explain why the session is not being maintained when I am doing a forward in a servlet after setting a cookie. I am even unable to set session attributes or parameters and...
6
4448
by: Sushmita | last post by:
hi all, I have wrriten code for a small web application. From my JSP page i am not able to call the servlet.. its throwing an exception. "javax.servlet.ServletException: Wrapper cannot find...
1
2130
by: shiyamala | last post by:
Hi i am shiyam, i am having some problem in java, i am basic java programmer. i have to write one program is writing the data into textfile name "newfile.txt" from one servlet and read it from...
2
3426
by: ManidipSengupta | last post by:
Hi, a few (3) questions for the Java experts, and let me know if this is the right forum. It deals with 100% java code (reason for posting here) but manages a Web browser with Javascript. Thanks in...
9
3146
by: mjahabarsadiq | last post by:
Hi I have created a servlet that is to be started at the server startup. And I got it. In that I have created a object of another class and set it as a session attribute. What I am trying is...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.