Advertisement
dzodzo

Log

Jan 31st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.16 KB | None | 0 0
  1. index.jsp
  2.  
  3.  
  4. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  5. <!DOCTYPE html>
  6. <html>
  7.     <head>
  8.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  9.         <title>JSP Page</title>
  10.     </head>
  11.     <body>
  12.        
  13.         <a href="unos.jsp">Unos</a><br/>
  14.         <a href="ServletKola?ispis=lista">Lista</a><br/>
  15.         <a href="ServletKola?ispis=jedan&br=1">Jedan</a><br/>
  16.        
  17.     </body>
  18. </html>
  19.  
  20. -------
  21. indexLog.jsp
  22.  
  23. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  24. <!DOCTYPE html>
  25. <html>
  26.     <head>
  27.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  28.         <title>IndexLog</title>
  29.     </head>
  30.     <body>
  31.         <form action="ServletLog" method="post">
  32.             Ime:<input type="text" name="ime"><br/>
  33.             Prezime:<input type="text" name="prezime"><br/>
  34.             <input type="submit" value="Login"><br/>
  35.         </form>
  36.        
  37.         <%
  38.             String poruka = (String)request.getAttribute("poruka");
  39.             if(poruka != null){
  40.                 %>
  41.                 <%=poruka%>
  42.           <%}%>
  43.          
  44.     </body>
  45. </html>
  46. -----------------------
  47. kola.jsp
  48.  
  49.  
  50. <%@page import="car.Automobili"%>
  51. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  52. <!DOCTYPE html>
  53. <html>
  54.     <head>
  55.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  56.         <title>JSP Page</title>
  57.     </head>
  58.     <body>
  59.         <%
  60.             Automobili a = (car.Automobili)request.getAttribute("kola");
  61.             %>
  62.             <table>
  63.                 <tr>
  64.                 <td>Marka" </td>
  65.                <td>Cena: </td>
  66.                <td>Brzina: </td>
  67.                </tr>
  68.                <tr>
  69.                    <td><%=a.getMarka()%></td>
  70.                    <td><%=a.getCena()%></td>
  71.                    <td><%=a.getBrzina()%></td>
  72.                </tr>
  73.            </table>
  74.    </body>
  75. </html>
  76.  
  77. ------------
  78. listaKola.jsp
  79. <%@page import="car.Automobili,java.util.ArrayList"%>
  80. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  81. <!DOCTYPE html>
  82. <html>
  83.    <head>
  84.        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  85.        <title>JSP Page</title>
  86.    </head>
  87.    <body>
  88.       <%
  89.           ArrayList<Automobili> au = (ArrayList<Automobili>)request.getAttribute("listaKola");
  90.           %>
  91.           <table>
  92.                   <tr>
  93.                       <td>Marka: </td>
  94.                       <td>Cena: </td>
  95.                       <td>Brzina: </td>
  96.                   </tr>
  97.                   <% for(Automobili pom:au){%>
  98.                    <tr>
  99.                       <td><%=pom.getMarka()%> </td>
  100.                       <td><%=pom.getCena()%></td>
  101.                       <td><%=pom.getBrzina()%></td>
  102.                   </tr>
  103.                   <%}%>
  104.    </body>
  105. </html>
  106. ------------------
  107. unos.jsp
  108.  
  109. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  110. <!DOCTYPE html>
  111. <html>
  112.    <head>
  113.        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  114.        <title>Unos</title>
  115.    </head>
  116.    <body>
  117.        <form action="ServletKola" method="POST">
  118.            Marka: <input type="text" name="marka"><br/>
  119.            Cena: <input type="text" name="cena"><br/>
  120.            Brzina: <input type="text" name="brzina"><br/>
  121.            <input type="submit" value="unos"><br/>
  122.        </form>
  123.        
  124.        <%
  125.            String poruka =(String)request.getAttribute("poruka");
  126.            if( poruka != null){
  127.                %>
  128.                <%=poruka%>
  129.                <%}%>
  130.            
  131.    </body>
  132. </html>
  133. ------------------------------------
  134. Automobili.java
  135.  
  136. package car;
  137.  
  138.  
  139. public class Automobili {
  140.   private String marka;
  141.   private int cena;
  142.   private int brzina;
  143.  
  144.    public Automobili() {
  145.    }
  146.  
  147.    public Automobili(String marka, int cena, int brzina) {
  148.        this.marka = marka;
  149.        this.cena = cena;
  150.        this.brzina = brzina;
  151.    }
  152.  
  153.    public String getMarka() {
  154.        return marka;
  155.    }
  156.  
  157.    public void setMarka(String marka) {
  158.        this.marka = marka;
  159.    }
  160.  
  161.    public int getCena() {
  162.        return cena;
  163.    }
  164.  
  165.    public void setCena(int cena) {
  166.        this.cena = cena;
  167.    }
  168.  
  169.    public int getBrzina() {
  170.        return brzina;
  171.    }
  172.  
  173.    public void setBrzina(int brzina) {
  174.        this.brzina = brzina;
  175.    }
  176.  
  177.  
  178. }
  179. -------------
  180. ServletKola.java
  181.  
  182. package car;
  183.  
  184. import java.io.IOException;
  185. import java.io.PrintWriter;
  186. import java.util.ArrayList;
  187. import javax.servlet.ServletException;
  188. import javax.servlet.http.HttpServlet;
  189. import javax.servlet.http.HttpServletRequest;
  190. import javax.servlet.http.HttpServletResponse;
  191.  
  192.  
  193. public class ServletKola extends HttpServlet {
  194.  
  195.    protected void doGet(HttpServletRequest request, HttpServletResponse response)
  196.            throws ServletException, IOException {
  197.       ArrayList<Automobili> au = new ArrayList<Automobili>();
  198.       au.add(new Automobili("McLaren P1",100000,350));
  199.       au.add(new Automobili("Lamborghini Aventador SV",10000,300));
  200.       au.add(new Automobili("Mercedes-Benz S-Class Long AMG",1000000,350));
  201.       String ispis = request.getParameter("ispis");
  202.       if(ispis !=null){
  203.           if(ispis.equals("lista")){
  204.               request.setAttribute("listaKola", au);
  205.               request.getRequestDispatcher("listaKola.jsp").forward(request, response);
  206.           }
  207.           else if(ispis.equals("jedan")){
  208.               String br = request.getParameter("br");
  209.               if(br !=null){
  210.                   request.setAttribute("kola",au.get(new Integer(br)));
  211.                   request.getRequestDispatcher("kola.jsp").forward(request, response);
  212.               }
  213.           }
  214.       }else{
  215.               request.setAttribute("poruka", "Nema sta da se izbaci");
  216.               request.getRequestDispatcher("index.jsp").forward(request, response);
  217.            }
  218.      
  219.    }
  220.  
  221.  
  222.    protected void doPost(HttpServletRequest request, HttpServletResponse response)
  223.            throws ServletException, IOException {
  224.        String marka = request.getParameter("marka");
  225.        String cena = request.getParameter("cena");
  226.        String brzina= request.getParameter("brzina");
  227.        
  228.        if(marka != null && cena != null && brzina != null){
  229.            try{
  230.            int Cena,Brzina;
  231.            Cena = Integer.parseInt(cena);
  232.            Brzina = Integer.parseInt(brzina);
  233.            Automobili a = new Automobili(marka, Cena, Brzina);
  234.            request.setAttribute("kola", a);
  235.            request.getRequestDispatcher("kola.jsp").forward(request, response);
  236.        }catch(Exception e){
  237.            request.setAttribute("poruka", "Greska");
  238.            request.getRequestDispatcher("unos.jsp").forward(request, response);
  239.        }
  240.        }else{
  241.             request.setAttribute("poruka", "Morate popuniti sva polja");
  242.            request.getRequestDispatcher("unos.jsp").forward(request, response);
  243.        }  
  244.    }
  245. }
  246. ------------------
  247. ServletLog.java
  248.  
  249. package car;
  250.  
  251. import java.sql.Connection;
  252. import java.sql.DriverManager;
  253. import java.sql.PreparedStatement;
  254. import java.sql.ResultSet;
  255. import java.io.IOException;
  256. import java.io.PrintWriter;
  257. import javax.servlet.ServletException;
  258. import javax.servlet.http.HttpServlet;
  259. import javax.servlet.http.HttpServletRequest;
  260. import javax.servlet.http.HttpServletResponse;
  261.  
  262.  
  263. public class ServletLog extends HttpServlet {
  264.  
  265.    
  266.    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  267.            throws ServletException, IOException {
  268.        response.setContentType("text/html;charset=UTF-8");
  269.        try (PrintWriter out = response.getWriter()) {
  270.         String ime = request.getParameter("ime");
  271.         String prezime = request.getParameter("prezime");
  272.         if( ime.isEmpty() || prezime.isEmpty()){
  273.             request.setAttribute("poruka", "Morate popuniti sva polja");
  274.             request.getRequestDispatcher("indexLog.jsp").forward(request, response);
  275.         }
  276.         try{
  277.             Class.forName("com.mysql.jdbc.Driver");
  278.             Connection kon = DriverManager.getConnection("jdbc:mysql://localhost:3306/korisnik","root","");
  279.              PreparedStatement ps = kon.prepareStatement("select ime,prezime from korisnikinfo where ime=? and prezime= ?");
  280.              ps.setString(1, ime);
  281.              ps.setString(2, prezime);
  282.              ResultSet rs = ps.executeQuery();
  283.              if(rs.next()){
  284.                  response.sendRedirect("index.jsp");
  285.              }else{
  286.                  request.setAttribute("poruka", "Morate popuniti sva polja");
  287.              request.getRequestDispatcher("indexLog.jsp").forward(request, response);
  288.              }
  289.              kon.close();
  290.              
  291.          }catch(Exception e){
  292.            
  293.          }
  294.         }
  295.     }
  296.  
  297.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  298.     /**
  299.      * Handles the HTTP <code>GET</code> method.
  300.      *
  301.      * @param request servlet request
  302.      * @param response servlet response
  303.      * @throws ServletException if a servlet-specific error occurs
  304.      * @throws IOException if an I/O error occurs
  305.      */
  306.     @Override
  307.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  308.             throws ServletException, IOException {
  309.         processRequest(request, response);
  310.     }
  311.  
  312.     /**
  313.      * Handles the HTTP <code>POST</code> method.
  314.      *
  315.      * @param request servlet request
  316.      * @param response servlet response
  317.      * @throws ServletException if a servlet-specific error occurs
  318.      * @throws IOException if an I/O error occurs
  319.      */
  320.     @Override
  321.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  322.             throws ServletException, IOException {
  323.         processRequest(request, response);
  324.     }
  325.  
  326.     /**
  327.      * Returns a short description of the servlet.
  328.      *
  329.      * @return a String containing servlet description
  330.      */
  331.     @Override
  332.     public String getServletInfo() {
  333.         return "Short description";
  334.     }// </editor-fold>
  335.  
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement