import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SimpleServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; response.setContentType("text/html"); out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println("Hello world servlet"); out.println("</TITLE></HEAD><BODY>"); out.println("<H1>" + title + "</H1>"); out.println("<P>This is output from SimpleServlet."); out.println("</BODY></HTML>"); out.close(); } } |
<html> <head><title>Formularz</title></head> <body> <form action="/karolb/test" method="post"> Podaj tekst: <input type="text" name="tekst" value=""> </form> </body> </html> |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class TestServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String tekst; response.setContentType("text/html"); out = response.getWriter(); out.println("<HTML><HEAD><TITLE>Wyniki z formularza"); out.println("</TITLE></HEAD><BODY>"); tekst = request.getParameter("tekst"); if (tekst != null) out.println("Z formularza otrzymalem: " + tekst); else out.println("Brak danych z formularza"); out.println("</BODY></HTML>"); out.close(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out; String tekst; response.setContentType("text/html"); out = response.getWriter(); out.println("<HTML><HEAD><TITLE>Uwaga</TITLE>"); out.println("</HEAD><BODY>"); out.println("Ten serwlet nie obsluguje metody GET!"); out.println("</BODY></HTML>"); out.close(); } } |
String wartosc = "abc"; out.println("Link do drugiego serwletu z parametrem: " + ... "<a href=\"" + response.encodeURL("/drugi_serwlet?param=" + wartosc) + "\">" + ...); |
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DodajProdukt extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(true); Koszyk koszyk = (Koszyk) session.getAttribute("pl.zakupy.koszyk"); if (koszyk == null) { koszyk = new Koszyk(); session.setAttribute("pl.zakupy.koszyk", koszyk); } koszyk.dodaj(request.getParameter("produkt")); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher( "/wyswietl_koszyk.jsp"); if (dispatcher != null) dispatcher.forward(request, response); } } |
String wartosc = "byle-co"; Cookie ciacho = new Cookie("Nazwa", wartosc); response.addCookie(ciacho); |
Cookie[] cookies = request.getCookies(); String wartosc; for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("Nazwa")) wartosc = thisCookie.getValue(); } |
public class WyswietlDane extends HttpServlet { public synchronized void init() throws ServletExcpetion { BazaDanych bd = (BazaDanych) getServletContext().getAttribute("pl.sklep.bd.baza"); if (bd == null) { bd = new BazaDanych("http://pulsar", "scott", "tiger"); getServletContext().setAttribute("pl.sklep.bd.baza", bd); } } ... } |
public final class HitCounterFilter implements Filter { private FilterConfig filterConfig = null; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; } public void destroy() { this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (filterConfig == null) return; StringWriter sw = new StringWriter(); PrintWriter writer = new PrintWriter(sw); Counter counter = (Counter)filterConfig. getServletContext(). getAttribute("hitCounter"); writer.println(); writer.println("==============="); writer.println("The number of hits is: " + counter.incCounter()); writer.println("==============="); // Log the resulting string writer.flush(); filterConfig.getServletContext(). log(sw.getBuffer().toString()); ... chain.doFilter(request, wrapper); ... } } |
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name> test_serw </servlet-name> <servlet-class> Test </servlet-class> </servlet> <servlet-mapping> <servlet-name> test_serw </servlet-name> <url-pattern> /test </url-pattern> </servlet-mapping> </web-app> |
<Context path="/test_kat" docBase="webapps/test_kat" reloadable="true"/> |