0

Hello All,

Can anybody plz guide me how to write a new method in servlet. I am using netbeans 6.8
A code snippet will be highly appreciated.
Also I will be calling these method of servlet B from servlet A. In Servlet A there are 2 hyper links, which should display appropriate contents.
I want to generate these contents through method.
Any suggestion will be highly appreciated.
Thank You,
Maya

3
Contributors
5
Replies
12
Views
7 Years
Discussion Span
Last Post by mayaaa
0

Well i am not sure if we can directly call a user-defined method of one servlet from another servlet....but we can achieve this in this way....I have defined an user defined method in servletB and called it in doGet() lifecycle method of SerletB and from the ServletA's life cycle method i created a RequestDispatcher object and used the method rd.include(req,res)...and it worked fine.....Here's the code...

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletA extends HttpServlet{

	public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
		PrintWriter pw=res.getWriter();
		pw.println(" This is service method of ServletA ");
		ServletContext ctx=getServletContext();
		RequestDispatcher rd=ctx.getRequestDispatcher("/tt");
		rd.include(req,res);
	}//end of doGet


}//endof servlet A
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletB extends HttpServlet{
	PrintWriter pw;
	

	public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
		
		
		pw=res.getWriter();
		pw.println(" This is service method of servletB and i am being called by ServletA ");
		hello();
		
	}
	public void hello(){
		pw.println(" This is hello method of serveltB and i am being called in service method ");
	}
}//end of ServletB

web.xml file

<web-app>
  <servlet>
     <servlet-name>serva</servlet-name>
	 <servlet-class>ServletA</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>serva</servlet-name>
	 <url-pattern>/ss</url-pattern>
  </servlet-mapping>

  <servlet>
     <servlet-name>servb</servlet-name>
	 <servlet-class>ServletB</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>servb</servlet-name>
	 <url-pattern>/tt</url-pattern>
  </servlet-mapping>
</web-app>

Hope this is the solution to ur question....

Edited by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

0

Hye thanks buddy

:)

Actually I am trying to write a method which has two arrays and a if loop. I neet to ecxute the If loop condition on a button click, and I am writing this in servlet.
Can u plz help?

0

Hi you should take a little bit pain to tell me what type of arrays are you taking and what is the if condition doing there?.....If you post the question in a vague form no one will first understand ur question to help you...thanks...

0

Also what you want will not be in a separate method but you will submit the form "towards" the servlet when you click the button and execute the code inside the toGet method of the servlet

0

Also what you want will not be in a separate method but you will submit the form "towards" the servlet when you click the button and execute the code inside the toGet method of the servlet

hye thanks for the replies.
Ok I will be more elaborative.
I am writing a servlet for login, where I need to verify the user. To verify the user I need to submit some valid usernames and passwords in two arrays. And then if the entered usernames and passwors matches, the user will be taken to next step, else a validation message should be displayed.

This is the javascript which i designed. But now I want to write it in servlet.

<script type="text/javascript">

function hr(form)

{
var uname= new Array("jojo","tim");

var pswrd= new Array("jojo1","tim1");

if
(form.uname.value == uname[0] && form.password.value == pswrd[0] || form.uname.value == uname[1] && form.pswrd.value ==pswrd[1] )

{
window.location="http://google.com";
}

else

{
alert("Invalid Username or password!");
}


}


</script>

Edited by peter_budo: Keep it Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.