Solved

how to call java class from servlet

Posted on 2007-11-27
3
4,399 Views
Last Modified: 2011-04-14
hi there
I am learning servlet and trying to do a form that has a button submit, when users hit the submit button it will print out the form to html and save the value of the form to a file. I have wrote a separate class that write the value to a file already. However how would you program the submit button to both submit the form and call the class that I wrote to write to file


thanks

Also how would you take out value of that file then put back to the form if users hit the load button

thanks again
0
Comment
Question by:dminh01
3 Comments
 
LVL 9

Accepted Solution

by:
ysnky earned 500 total points
ID: 20363660
a simple servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
 
         handleData (req, res);   // handle post requests
    }

   public void doGet(HttpServletRequest req, HttpServletResponse res)
                               throws ServletException, IOException {
     
      handleData (req, res);   // handle get requests
   }

   private void handleData (HttpServletRequest req, HttpServletResponse res) {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println("<HTML>");
    out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<BIG>Hello World</BIG>");
    out.println("</BODY></HTML>");
  }
}


for more info;
http://www.cs.usfca.edu/~parrt/course/601/lectures/servlets.html
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html
http://www.unix.org.ua/orelly/java-ent/servlet/ch02_01.htm
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20363837
>>However how would you program the submit button to both submit the form and call the class that I wrote to write to file

It's not the form that would call the class - it's whatever receives the request at the server. What is doing so at the moment? If it's a servlet, load the class in doPost and process the file there
0
 
LVL 16

Expert Comment

by:ellandrd
ID: 20363875
can we see your code first please?

>>print out the form to html

do you mean take the form values and display them in the HTML page?

>>and call the class that I wrote to write to file

why are you calling another class to do your input output operations?  

Why not just do everything in one file?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
site launch date and last modified date 3 80
simple java question 3 44
replace a word with other 1 40
Configure a Bean in an XML file 4 9
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now