473,625 Members | 3,064 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 1209

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

Similar topics

1
3890
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 C:\Tomcat4\ with C:\Tomcat4\webapps\myhome\WEB-INF\classes to store all my java.class files. Under WEB-INF, I've have edit the web.xml file as <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD...
2
2464
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 web xml <servet-mapping> is :
7
3595
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: javax.servlet.ServletException: java.sql.SQLException: Communication link failure: java.io.IOException, underlying cause: Unexpected end of input stream
14
2398
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 form.here is my code:(simple code just for testing i did this program) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title...
0
2643
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 do a forward from a servlet to another servlet via a JSP page, the attributes seize to exist. I still do not understand the concept of session tracking. First page JSP: _______________________________________________ <%@ page...
6
4466
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 servlet class servlet.IssueTrackerControllerServlet or a class it depends on org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39) ...
1
2140
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 another servlet its work but i want to know hw to deploy the readed input from textfile is displayed in jsp through servlet2. here is my code import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import...
2
3448
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 advance for responding. I have made a stub of the codes for better understanding. Situation: I have a Java servlet (HelloWorldServlet) running on a Tomcat (5.5.26) server on a Linux machine (compiled in JDK1.4.2). It writes Javascript to construct...
9
3167
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 to get the object in a jsp page. Following are the codes.
0
8259
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8696
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7188
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.