Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ultra simple examples of HttpServlet, DesignerFacesServlet, and AbstractXSPServlet.
package com.hello.servlets;
import java.util.Enumeration;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.commons.util.StringUtil;
import frostillicus.xsp.servlet.AbstractXSPServlet;
/**
* Example Servlet implementing AbstractXSPServlet,
* from Jesse Gallagher.
*
* @author Eric McCormick, @edm00se
*
*/
public class ExampleAbstractedServlet extends AbstractXSPServlet {
/* (non-Javadoc)
* @see frostillicus.xsp.servlet.AbstractXSPServlet#doService(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, javax.faces.context.FacesContext, javax.servlet.ServletOutputStream)
*/
@Override
protected void doService(HttpServletRequest req, HttpServletResponse res,
FacesContext facesContext, ServletOutputStream out)
throws Exception {
res.setContentType("text/plain");
String reqMethod = req.getMethod();
if( reqMethod.equals("GET") ) {
out.println("doing something with GET");
} else if( reqMethod.equals("POST") ) {
res.setStatus(200); // OK
out.println("doing something with POST");
} else if( reqMethod.equals("PUT") ) {
res.setStatus(200); // OK
out.println("doing something with PUT");
} else if( reqMethod.equals("DELETE") ) {
res.setStatus(200); // OK
out.println("doing something with DELETE");
} else if( reqMethod.equals("PATCH") ) {
res.setStatus(200); // OK
out.println("doing something with PATCH");
} else {
res.setStatus(405); // method not allowed
out.println("what the devil are you trying to do, break the server?");
}
}
}
package com.hello.servlets;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Map;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.xsp.webapp.DesignerFacesServlet;
/**
* Example servlet showing the use of access to FacesContext.
* This was originally blogged about by Jesse Gallagher and
* is a near duplicate class.
* src: https://frostillic.us/blog/posts/159496067A27FD3585257A70005E7BC1
*/
public class ExampleServlet extends DesignerFacesServlet {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
// Set up handy environment variables
HttpServletRequest req = (HttpServletRequest)servletRequest;
HttpServletResponse res = (HttpServletResponse)servletResponse;
ServletOutputStream out = res.getOutputStream();
FacesContext facesContext = this.getFacesContext(req, res);
try {
res.setContentType("text/plain");
out.println("start");
// The sessionScope is available via the ExternalContext. Resolving the variable
// would work as well
Map<Object, Object> sessionScope = facesContext.getExternalContext().getSessionMap();
// ... this is showing how we can get facesContext and *scope variables inside the servlet
out.println("done");
} catch(Exception e) {
// hit an error, dump out whatever is there
e.printStackTrace(new PrintStream(out));
} finally {
out.close();
// It shouldn't be null if things are going well, but a check never hurt
if(facesContext != null) {
//complete the response and release the handle on the FacesContext instance
facesContext.responseComplete();
facesContext.release();
}
}
}
/**
* @return FacesContext currentInstance
*/
public static FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
}
package com.hello.servlets;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Example Servlet, implemented as 'vanilla'
* HttpServlet. Many non-Domino Java Servlets
* implement HttpServlet.
*
* @author Eric McCormick, @edm00se
*
*/
public class ExampleHttpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init () {
}
@Override
public void destroy () {
}
@Override
protected void doGet (HttpServletRequest req, HttpServletResponse res) {
HttpServletRequest _req = req;
HttpServletResponse _res = res;
PrintWriter out = null;
try{
_res.setContentType("text/plain");
out = _res.getWriter();
out.println("Servlet Running");
out.println("You executed a "+_req.getMethod().toString()+" request.");
}catch(Exception e){
if(out!=null){
out.println("Sorry, an error occurred...");
}
}finally{
out.close();
}
}
}
String reqMethod = req.getMethod();
if( reqMethod.equals("GET") ) {
try {
out.println("doing something with my GET");
} catch (Exception e) {
e.printStackTrace();
out.println(e.toString());
} finally {
facesContext.responseComplete();
facesContext.release();
out.close();
}
} else if( reqMethod.equals("POST") ) {
try {
out.println("doing something with my POST");
} catch (Exception e) {
e.printStackTrace();
out.println(e.toString());
} finally {
facesContext.responseComplete();
facesContext.release();
out.close();
}
} //...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.