Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Leon Hansen   on Jul 05 In Java Category.

  
Question Answered By: Perdita Lopez   on Jul 05

In here you can find  one sample :


web.xml
<servlet>
<servlet-name>FrontServlet</servlet-name>
<servlet-class>ir.nsy.dlib.com.frontcontroller.FrontServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FrontServlet</servlet-name>
<url-pattern>/frontServlet</url-pattern>
</servlet-mapping>

the code of servlet
public class FrontServlet extends HttpServlet {
//private static final String CONTENT_TYPE = "text/html; charset=windows-1252";

public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request , response);
try {
RequestContext ctx = new RequestContextImp(request, response);
ctx.initialize(request);
ctx.setRequest(request);
ctx.setResponse(response);
ctx.setServletContext(getServletContext());
WebCommandFactory.getApplicationControllerCommand(ctx.getCommandName()).handleRequest(ctx);
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request , response);
try {
RequestContext ctx = new RequestContextImp();
ctx.initialize(request);
ctx.setRequest(request);
ctx.setResponse(response);
ctx.setServletContext(getServletContext());
WebCommandFactory.getApplicationControllerCommand(ctx.getCommandName()).handleRequest(ctx);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
}
for navigate to specific jsp  do like down:
static class DispatcherViewImpl implements DispatcherView {
public void dispatch(HttpServletRequest request, HttpServletResponse response, final String page, ServletContext ctx)
throws javax.servlet.ServletException, java.io.IOException {
try {
RequestDispatcher dispatcher = ctx.getRequestDispatcher(page);
dispatcher.forward(request, response);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Share: 

 

This Question has 2 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Servlet Definition Or get search suggestion and latest updates.


Tagged: