Hi Sachin
I tried to compile the program below using javac, and it compiled fine. I
then opened up my browser and keyed in "http://localhost:8080/MyServlet" but
it does not print the file that I ask it to read, but instead returns
"null". I struggled with it
for 5-6 days, no luck.
Can you kindly help!
Thanks & Regards
Ken
>
>import java.io.*;
>import java.util.Enumeration;
>import java.awt.*;
>import javax.servlet.*;
>import javax.servlet.http.*;
>import java.awt.event.*;
>import javax.swing.*;
>
>/**
>* This is a simple example of an HTTP Servlet that
>uses the HttpSession
>* class
>*
>* Note that in order to gaurentee that session
>response headers are
>* set correctly, the session must be retrieved before
>any output is
>* sent to the client.
>*/
>public class MyServlet extends HttpServlet {
>
>
> JTextArea theTextArea = new JTextArea(20, 64);
> JButton theReadButton = new JButton("Read");
> String filename;
>
> public void doGet (HttpServletRequest req,
>HttpServletResponse res)
> throws ServletException, IOException
> {
> //Get the session object
> HttpSession session = req.getSession(true);
> JFrame frm = new JFrame();
> Container c = frm.getContentPane();
> theTextArea.setFont(new Font("Monospaced",
>Font.BOLD, 14));
> c.add(new JScrollPane(theTextArea,
> JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
>
>JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS),
> "Center");
>
> c.add(theReadButton, "South");
> filename = req.getParameter("filename");
>
> theReadButton.addActionListener(new
>ActionListener()
> {
> public void actionPerformed(ActionEvent evt)
> {
>
> FileReader in = null;
> try
> {
> in = new FileReader(filename);
>
> int ch;
> while ( (ch = in.read()) != -1 )
> {
> theTextArea.append("" + (char) ch);
> }
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> finally
> {
> try
> {
> if (in != null) in.close();
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> }
> }
> });
>frm.show();
>
> // set content type and other response header
>fields first
> res.setContentType("text/html");
>
> // then write the data of the response
> PrintWriter out = res.getWriter();
> out.println("<html>");
> out.println("<HEAD>");
>
> out.println("<title>the title of my
>document</title>");
>
> out.println("</head>");
>
> out.println("<body> + filename + </body>");
> out.close();
>
>
>
> }
>
>}
_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets