Last active
July 21, 2023 21:32
-
-
Save edm00se/fd47302a1918c93a262f to your computer and use it in GitHub Desktop.
Ultra simple examples of HttpServlet, DesignerFacesServlet, and AbstractXSPServlet.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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