Wednesday, March 24, 2010

Navigating from an ordinary java class(POJO) to a JSP/Servlet.

1.TestURLConn.java
------------------
package com;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestURLConn {

HttpServletRequest req;
HttpServletResponse resp;

public TestURLConn(HttpServletRequest req,HttpServletResponse resp){

this.req=req;
this.resp=resp;
}
public void getControl(){

try {
this.req.setAttribute("name","sriRama");
req.getRequestDispatcher("TestURLConn.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
---------------------------------------------------------------------------

2. index.jsp
-------------
<% request.setAttribute("val","srirama navami sadsad"); TestURLConn tuc=new TestURLConn(request,response); tuc.getControl(); %>
---------------------------------------------------------------------------
3.TestURLConn.jsp
-----------------


this is TestURLConn.jsp


<%if(request.getAttribute("val").toString()!=null){ System.out.println(request.getAttribute("val").toString()); out.println(request.getAttribute("val").toString()); }%>


<%if(request.getAttribute("name").toString()!=null){ System.out.println(request.getAttribute("name").toString()); out.println(request.getAttribute("name").toString()); } %>

----------------------------------------------------
out put
-------
srirama navami
sriRama

No comments: