ddeexxiikk

Ti-LAB3.java

Oct 27th, 2025
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | Source Code | 0 0
  1. package pk.wieik.ti;
  2.  
  3. import java.io.*;
  4.  
  5. import jakarta.servlet.ServletContext;
  6. import jakarta.servlet.http.*;
  7. import jakarta.servlet.annotation.*;
  8. import pk.wieik.ti.model.JBuzytkownik;
  9. import pk.wieik.ti.model.Narzedzia;
  10.  
  11. @WebServlet(name = "helloServlet", value = "/hello-servlet")
  12. public class HelloServlet extends HttpServlet {
  13.     private String message;
  14.  
  15.     public void init() {
  16.         message = "Hello World!";
  17.     }
  18.  
  19.     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
  20.         response.setContentType("text/html");
  21.         response.setCharacterEncoding("UTF-8");
  22.         ServletContext context = getServletContext();
  23.         PrintWriter out = response.getWriter();
  24.  
  25.         HttpSession sesja = request.getSession();
  26.         String atrybut1 = (String) sesja.getAttribute("atrybut1");
  27.         Integer atrybut2 = (Integer) sesja.getAttribute("atrybut2");
  28.  
  29.         if (atrybut1 == null)
  30.             atrybut1 = "";
  31.  
  32.         if (atrybut2 == null)
  33.             atrybut2 = 0;
  34.  
  35.  
  36.         JBuzytkownik uzytkownik = (JBuzytkownik) sesja.getAttribute("uzytkownik");
  37.         if (uzytkownik == null) {
  38.             uzytkownik = new JBuzytkownik();
  39.             sesja.setAttribute("uzytkownik", uzytkownik);
  40.         }
  41.  
  42.         String strona = request.getParameter("strona");
  43.         if (uzytkownik.getUprawnienia()>0)
  44.             strona = Narzedzia.parsujStrone(strona, "glowna;kwadratowe;trzecia;ustawienia");
  45.         else
  46.             strona = Narzedzia.parsujStrone(strona, "glowna;kwadratowe;trzecia");
  47.  
  48.  
  49.         String szablon = Narzedzia.pobierzSzablon("index.html", context);
  50.     szablon = Narzedzia.uzupelnij(szablon, "NAGLOWEK", "naglowek.html", context);
  51.     szablon = Narzedzia.uzupelnij(szablon, "MENU", "menu.html", context);
  52.  
  53.     String authHtml = "";
  54.     if (uzytkownik.getUprawnienia() > 0) {
  55.         authHtml = "<li><a href='?strona=ustawienia'>Ustawienia</a></li>" +
  56.             "</ul><div style='background:#e9e9e9;padding:8px;margin-top:6px;'>" +
  57.             "Jesteś zalogowany jako <strong>" + uzytkownik.getLogin() + "</strong> " +
  58.             "<form method='post' action='JB' style='display:inline;'>" +
  59.             "<input type='hidden' name='akcja' value='logout'/>" +
  60.             "<input type='submit' value='Wyloguj'/>" +
  61.             "</form></div>";
  62.     } else {
  63.         authHtml = "</ul><div style='background:#e9e9e9;padding:8px;margin-top:6px;'>" +
  64.             "<form method='post' action='JB'>" +
  65.             "<label>Login:<br/><input type='text' name='login'/></label><br/>" +
  66.             "<label>Hasło:<br/><input type='password' name='haslo'/></label><br/>" +
  67.             "<input type='hidden' name='akcja' value='login'/>" +
  68.             "<input type='submit' value='Zaloguj'/>" +
  69.             "</form></div>";
  70.     }
  71.    
  72.     szablon = szablon.replace("[[AUTH]]", authHtml);
  73.         szablon = Narzedzia.uzupelnij(szablon, "TRESC", "tresc.html", context);
  74.         szablon = Narzedzia.uzupelnij(szablon, "STOPKA", "stopka.html", context);
  75.         out.println(szablon);
  76.         out.close();
  77.     }
  78.     public void destroy() {
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment