?
Solved

how to call java class from servlet

Posted on 2007-11-27
3
Medium Priority
?
4,421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
ysnky earned 1500 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 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