Solved

how to call java class from servlet

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Developer tools in browsers have been around for a while, yet they are still heavily underused by developers. Developers still fix html or CSS then refresh page to see effect, or they put alert or debugger in JavaScript and then try again and again …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

707 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