Close

Java Servlet - @WebServlet Examples

Java Servlet JAVA EE 

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "searchController", urlPatterns = {"/search/*"})
public class SearchController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
try {
//we are going to pause for a while
// to simulate the expensive search process
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
PrintWriter writer = resp.getWriter();
writer.println("Test Search Result");
}
}
Original Post




import com.google.common.cache.LoadingCache;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

@WebServlet(name = "appController", urlPatterns = {"/*"},
loadOnStartup = 1)
public class AppController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
Object cache = req.getServletContext().getAttribute("GLOBAL_CACHE");
if (cache instanceof LoadingCache) {
try {
resp.getWriter().println((((LoadingCache) cache).get("key")));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}

@Override
public void init() throws ServletException {
super.init();
System.out.println("servlet loaded " + this);
}
}
Original Post




import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "viewController", urlPatterns = {"/view/*"},
initParams = @WebInitParam(name = "renderer-class-name"
, value = "com.logicbig.HtmlRenderer"),
loadOnStartup = 1)
public class ViewController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {

String renderer = getServletConfig().getInitParameter("renderer-class-name");
PrintWriter writer = resp.getWriter();
writer.println("renderer: " + renderer);

String servletName = getServletConfig().getServletName();
writer.println("servlet name " + servletName);
}
}
Original Post




import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.ThreadLocalRandom;

@WebServlet(name = "myServlet", urlPatterns = {"/async-test"},
asyncSupported = true)
public class MyServlet extends HttpServlet {
@Override
protected void doGet (HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {

PrintWriter writer = resp.getWriter();
AsyncContext asyncContext = req.startAsync();
asyncContext.addListener(new MyAsyncListener());

AsyncContext finalAsyncContext = asyncContext;
asyncContext.start(new Runnable() {
@Override
public void run () {
String msg = task();
writer.println(msg);
finalAsyncContext.complete();
}
});
}

private String task () {
long start = System.currentTimeMillis();
try {
int i = ThreadLocalRandom.current()
.nextInt(1, 5);
Thread.sleep(i * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "time to complete long task " + (System.currentTimeMillis() - start);
}
}
Original Post
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "appController", urlPatterns = {"/*"},
loadOnStartup = 1)
public class AppController extends HttpServlet {
@Override
protected void doGet (HttpServletRequest req,
HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Request: " + req.getRequestURL());

resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();

if (req.getRequestURL()
.toString()
.contains("secondPage")) {
writer.write("You are on the second page<br/>");
writer.write("myAttribute = " + req.getSession(false)
.getAttribute("myAttribute"));

} else {
HttpSession session = req.getSession(true);
System.out.println("session id : " + session.getId());
session.setAttribute("myAttribute", "attrVal");
writer.write("<a href='" + req.getContextPath() + "/secondPage;jsessionid=" + session.getId() + "'>Visit the Second page</a>");
}

writer.close();

}
}
Original Post




See Also