[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4434
  • Last Modified:

how to call java class from servlet

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
dminh01
Asked:
dminh01
1 Solution
 
ysnkyCommented:
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
 
CEHJCommented:
>>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
 
ellandrdCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now