Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java method and servlet

Status
Not open for further replies.

kiki88

Programmer
Mar 24, 2003
1
DE
How to call a method of a java class in java servlet?
 
same way as you'd call any java class method.. after all a servlet is still a java class at the end of the day.

Example:




import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

AJavaClass myJavaClass;
/** Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {

super.init(config);
myJavaClass = new AJavaClass();
}

/** Destroys the servlet.
*/
public void destroy() {

}

/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
response.setContentType(&quot;text/html&quot;);
java.io.PrintWriter out = response.getWriter();

out.println(&quot;<html>&quot;);
out.println(&quot;<head>&quot;);
out.println(&quot;<title>Servlet</title>&quot;);
out.println(&quot;</head>&quot;);
out.println(&quot;<body>&quot;);
out.println(&quot;this is my class wossname&quot;+myJavaClass.toString()); // this is calling the toString() method in the instance of myJavaClass
out.println(&quot;</body>&quot;);
out.println(&quot;</html>&quot;);

out.close();
}

/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
processRequest(request, response);
}

/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return &quot;Short description&quot;;
}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top