Solved

how to call java class from servlet

Posted on 2007-11-27
3
4,398 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

13 Experts available now in Live!

Get 1:1 Help Now