Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- 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;
- import javax.servlet.http.HttpSession;
- /**
- *
- * @author student_10
- */
- public class NewServlet extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- try {
- out.println("<body>");
- //Pobierz obiekt sesji,bad zutworz jesli go nie ma
- HttpSession session = request.getSession(true);
- // Pobierz z sesji obiekt oznaczony etykieta zalogowany
- Boolean loggedIn = (Boolean)session.getAttribute("log");
- String action = request.getParameter("akcja");
- if (action != null){
- if(action.equals("Wyloguj")){
- Boolean war = false;
- session.setAttribute("log", war);
- }
- }
- //Jezeli w sesji nie bylo informacji o tym ze ktos
- // zostal zalgowoany
- if (loggedIn == null)
- loggedIn = false;
- if (loggedIn == true){
- out.println("<h1>ZALOGOWANO</h1>");
- out.println("<form method=\"post\">");
- out.println("<input type=\"hidden\" name=\"akcja\" value=\"Wyloguj\" \\>");
- out.println("<input type=\"submit\" value=\"Wyloguj\" \\>");
- out.println("</form>");
- }
- else
- { // uzytkownik nie zostal zalogowany
- out.println("<form method=\"post\">");
- out.println("Login: <input type=\"text\" name=\"user\">");
- out.println("Password: <input type=\"password\" name=\"pass\">");
- out.println("<input type=\"submit\" value=\"zaloguj\">");
- out.println("</form>");
- String user;
- String pass;
- user = request.getParameter("user");
- pass = request.getParameter("pass");
- if (user != null && pass != null){
- // uzytkownik probuje sie zalogowac
- if (user.equals("123") && pass.equals("123")){
- loggedIn = true;
- session.setAttribute("log", loggedIn);
- }
- }
- }
- out.println("</body>");
- }
- finally {
- out.close();
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement