otak info official Logo   
OTAK.INFO
Personal Blog

Hariyanto Lim

정길상 / 鄭吉祥
Home   Last Posts  
 
Please login to see more topics and discussion categories.
 
goto main category list >> Software programming >> ServLet

Title : To display jsp from ServLet
Total Reply : 0
Total View : 2485


Harry
Harry
Total thread: 131
Total reply: 50

Post #119
To display jsp from ServLet
Normally after we process the local inside ServLet, we can choose to do 1 of the action :

1. To display the HTML content directly inside ServLet by using
'
'.str_replace('
', ' ', '
public class MyServLet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

resp.setContentType("text/plain");
resp.getWriter().println("Helloworld !!!");
}
}
').'
'

2. We can use URL redirect (web URL will be changed)
'
'.str_replace('
', ' ', '
public class MyServLet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

// set parameter value to be processed on the new url
req.setAttribute("foo", "bar");

// point to the new url (redirect)
resp.sendRedirect("/hello/myjsp.jsp");
}
}
').'
'

3. In normal case we do NOT want to "redirect" (change URL) but instead we "forward" (show content of JSP)
'
'.str_replace('
', ' ', '
public class MyServLet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

// set parameter value to be processed on the new url
req.setAttribute("foo", "bar");

// get myjsp.jsp from inside "protected" (no direct access by visitor) folder "WEB-INF"
req.getRequestDispatcher("/WEB-INF/Myjsp.jsp").forward(req, resp);
}
}
').'
'
Falling in love with the world

Write : 2013-07-18 11:42:13

If you want to create a new reply then please login first.



www.OTAK.INFO
Since 19 January 2007
Page hit : 858,693

Code update 24th June 2013
Brain is a very capable to solve big problems
but requires constant reminders about how to.
peace bird