Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  6. <title>JSP Page</title>
  7. </head>
  8. <body>
  9. <h4> ${(poruka!=null) ? poruka: "Welcome!"} </h4> <br><br>
  10. <form action="Login" method="post">
  11. Korisnicko ime: <input type="text" name="username" value="${korisnik.username}"><br>
  12. Sifra: <input type="password" name="password" value="${korisnik.password}"><br>
  13. <select name="tip">
  14. <option value="kupac">kupac</option>
  15. <option value="prodavac">prodavac</option>
  16. </select><br><br>
  17.  
  18. <input type="submit" value="Prijava">
  19. <input type="reset" value="Reset">
  20.  
  21.  
  22. </form>
  23.  
  24. <h1 style="color: red">${greska}</h1>
  25.  
  26.  
  27. </body>
  28. </html>
  29.  
  30. package servlets;
  31.  
  32. import beans.Korisnik;
  33. import java.io.IOException;
  34. import java.io.PrintWriter;
  35. import javax.servlet.RequestDispatcher;
  36. import javax.servlet.ServletException;
  37. import javax.servlet.http.HttpServlet;
  38. import javax.servlet.http.HttpServletRequest;
  39. import javax.servlet.http.HttpServletResponse;
  40. import javax.servlet.http.HttpSession;
  41.  
  42. import java.sql.*;
  43. import java.util.logging.Level;
  44. import java.util.logging.Logger;
  45.  
  46. public class Login extends HttpServlet {
  47.  
  48. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  49. throws ServletException, IOException {
  50.  
  51. HttpSession session = request.getSession();
  52. response.setContentType("text/html;charset=UTF-8");
  53. PrintWriter out = response.getWriter();
  54.  
  55. String poruka = "";
  56. String adresa = "/index.jsp";
  57. String username = (String) request.getParameter("username");
  58. String password = (String) request.getParameter("password");
  59. String vrstaKorisnika = request.getParameter("tip");
  60.  
  61. Korisnik korisnik = new Korisnik();
  62. korisnik.setUsername(username);
  63. korisnik.setPassword(password);
  64.  
  65. session.setAttribute("korisnik", korisnik);
  66. if (username.equals("") || password.equals("")) {
  67.  
  68. poruka = "Unesite korisnicko ime i sifru!";
  69. request.setAttribute("poruka", poruka);
  70. RequestDispatcher reqDisp = request.getRequestDispatcher("index.jsp");
  71. reqDisp.forward(request, response);
  72. }
  73.  
  74. String myDriver = "com.mysql.jdbc.Driver";
  75. String myUrl = "jdbc:mysql://localhost/eprodavnica2017";
  76. String mySQLuserNameString = "root";
  77. String mySQLPasswordString = "root";
  78.  
  79. try {
  80. Class.forName(myDriver);
  81. } catch (ClassNotFoundException ex) {
  82. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  83. }
  84.  
  85. Connection connection;
  86. String query = "";
  87. Statement st;
  88. ResultSet rs;
  89.  
  90. try {
  91. connection = DriverManager.getConnection(myUrl, mySQLuserNameString, mySQLPasswordString);
  92. query = "select * from korisnik where username='" + username + "' and password='" + password + "';";
  93. st = connection.createStatement();
  94. rs = st.executeQuery(query);
  95.  
  96. if (rs.next() && vrstaKorisnika.equals("kupac")) {
  97.  
  98. String tip = rs.getString("type");
  99. if (tip.equals("kupac")) {
  100. adresa = "/kupac.jsp";
  101. st.close();
  102.  
  103. RequestDispatcher rd = request.getRequestDispatcher(adresa);
  104. rd.forward(request, response);
  105. }
  106.  
  107. }
  108.  
  109. connection = DriverManager.getConnection(myUrl, mySQLuserNameString, mySQLPasswordString);
  110. query = "select * from korisnik where username='" + username + "' and password='" + password + "';";
  111. st = connection.createStatement();
  112. rs = st.executeQuery(query);
  113.  
  114. if (rs.next() && vrstaKorisnika.equals("prodavac") && !vrstaKorisnika.equals("kupac")) {
  115.  
  116. String tip;
  117. try {
  118. tip = rs.getString("type");
  119. if (tip.equals("prodavac")) {
  120. adresa = "/prodavac.jsp";
  121. }
  122. } catch (SQLException ex) {
  123. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  124. }
  125.  
  126. } else {
  127.  
  128. poruka = "Neispravno korisnicko ime i lozinka! Pokusajte ponovo.";
  129. request.setAttribute("poruka", poruka);
  130. korisnik.setPassword("");
  131. adresa = "/index.jsp";
  132. //st.close();
  133. }
  134.  
  135. } catch (SQLException ex) {
  136. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  137. }
  138.  
  139. RequestDispatcher rd = request.getRequestDispatcher(adresa);
  140. rd.forward(request, response);
  141.  
  142. }
  143.  
  144. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  145. /**
  146. * Handles the HTTP <code>GET</code> method.
  147. *
  148. * @param request servlet request
  149. * @param response servlet response
  150. * @throws ServletException if a servlet-specific error occurs
  151. * @throws IOException if an I/O error occurs
  152. */
  153. /*
  154. @Override
  155. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  156. throws ServletException, IOException {
  157. try {
  158. processRequest(request, response);
  159. } catch (ClassNotFoundException ex) {
  160. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  161. } catch (SQLException ex) {
  162. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  163. }
  164. }*/
  165. /**
  166. * Handles the HTTP <code>POST</code> method.
  167. *
  168. * @param request servlet request
  169. * @param response servlet response
  170. * @throws ServletException if a servlet-specific error occurs
  171. * @throws IOException if an I/O error occurs
  172. */
  173. /*
  174. @Override
  175. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  176. throws ServletException, IOException {
  177. try {
  178. processRequest(request, response);
  179. } catch (ClassNotFoundException ex) {
  180. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  181. } catch (SQLException ex) {
  182. Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
  183. }
  184. }*/
  185. /**
  186. * Returns a short description of the servlet.
  187. *
  188. * @return a String containing servlet description
  189. */
  190. @Override
  191. public String getServletInfo() {
  192. return "Short description";
  193. }// </editor-fold>
  194.  
  195. }
  196.  
  197.  
  198. <%@page import="java.util.ArrayList"%>
  199. <%@page import="java.sql.*"%>
  200. <%@page import="java.util.logging.Logger"%>
  201. <%@page import="beans.Artikli"%>
  202. <%@page import="util.DB"%>
  203. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  204.  
  205.  
  206.  
  207.  
  208. <!DOCTYPE html>
  209.  
  210.  
  211. <%
  212.  
  213. ArrayList<Artikli> listaArtikala=new ArrayList<Artikli>();
  214.  
  215. String nazivProdavnice=request.getParameter("pretraga");
  216.  
  217. String query="";
  218.  
  219. query="select * from artikli;";
  220. if(request.getParameter("pretraga")!="")
  221. query="select * from artikli where naziv_prodavnice='" + nazivProdavnice + "';";
  222.  
  223. listaArtikala=DB.listaArtikala(query);
  224.  
  225.  
  226.  
  227. %>
  228.  
  229.  
  230. <html>
  231. <head>
  232. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  233. <title>Kupac</title>
  234. </head>
  235. <body>
  236.  
  237.  
  238. <table border="1">
  239. <tr>
  240. <td>Naziv</td>
  241. <td>Boja</td>
  242. <td>Kolicina</td>
  243. <td>Cena</td>
  244. </tr>
  245.  
  246. <% for(int i=0; i<listaArtikala.size(); i++) {
  247. %>
  248. <tr>
  249. <td><% out.println(listaArtikala.get(i).getNaziv()); %></td>
  250. <td><% out.println(listaArtikala.get(i).getBoja()); %></td>
  251. <td><% out.println(listaArtikala.get(i).getKolicina_na_lageru()); %></td>
  252. <td><% out.println(listaArtikala.get(i).getCena()); %></td>
  253.  
  254. </tr>
  255.  
  256. <% } %>
  257. </table>
  258.  
  259.  
  260.  
  261. <h4> ${(poruka!=null) ? poruka: "Stranica kupca!"} </h4> <br><br>
  262. <form action="kupac.jsp" method="post">
  263. Pretraga artikla: <input type="text" name="pretraga" value="${artikli.naziv_prodavnice}">
  264. <input type="submit" value="Submit">
  265.  
  266. </form>
  267. <br>
  268. <form action="Logout">
  269. <input type="submit" value="Logout">
  270.  
  271. </form>
  272.  
  273. </body>
  274. </html>
  275.  
  276.  
  277. <%@page import="util.DB"%>
  278. <%@page import="java.util.ArrayList"%>
  279. <%@page import="beans.Artikli"%>
  280. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  281. <!DOCTYPE html>
  282.  
  283. <%
  284.  
  285. ArrayList<Artikli> listaArtikala = new ArrayList<Artikli>();
  286.  
  287. String idArtikla = request.getParameter("pretraga");
  288. String kolLager = request.getParameter("kolicina");
  289.  
  290. int id=0;
  291. int kolicina=0;
  292.  
  293. if(idArtikla!=null && kolLager!=null){
  294.  
  295. id=Integer.parseInt(idArtikla);
  296. kolicina=Integer.parseInt(kolLager);
  297.  
  298. DB.izmeniKolicinuArtikala(id, kolicina);
  299.  
  300.  
  301. }
  302.  
  303. String query = "";
  304. query = "select * from artikli;";
  305.  
  306. listaArtikala = DB.listaArtikala(query);
  307.  
  308.  
  309. %>
  310.  
  311. <html>
  312. <head>
  313. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  314. <title>JSP Page</title>
  315. </head>
  316. <body>
  317. <table border="1">
  318. <tr>
  319. <td>ID</td>
  320. <td>Naziv</td>
  321. <td>Boja</td>
  322. <td>Kolicina</td>
  323. <td>Cena</td>
  324. </tr>
  325.  
  326. <% for (int i = 0; i < listaArtikala.size(); i++) {
  327. %>
  328. <tr>
  329.  
  330. <td><% out.println(listaArtikala.get(i).getId_artikal()); %></td>
  331. <td><% out.println(listaArtikala.get(i).getNaziv()); %></td>
  332. <td><% out.println(listaArtikala.get(i).getBoja()); %></td>
  333. <td><% out.println(listaArtikala.get(i).getKolicina_na_lageru()); %></td>
  334. <td><% out.println(listaArtikala.get(i).getCena()); %></td>
  335.  
  336. </tr>
  337.  
  338. <% }%>
  339. </table>
  340.  
  341. <br><br>
  342.  
  343. <form action="prodavac.jsp" method="post">
  344. ID artikla: <input type="text" name="pretraga" value="${artikli.id_artikal}">
  345. Kolicina: <input type="text" name="kolicina" value="${artikli.kolicina_na_lageru}">
  346. <input type="submit" value="Izmeni kolicinu">
  347.  
  348. </form>
  349.  
  350. </body>
  351. </html>
  352.  
  353.  
  354. package util;
  355.  
  356. import beans.Artikli;
  357. import java.util.ArrayList;
  358. import java.sql.*;
  359.  
  360.  
  361. public class DB {
  362.  
  363.  
  364.  
  365.  
  366. public static ArrayList<Artikli> listaArtikala(String query){
  367.  
  368. ArrayList<Artikli> listaArtikala = new ArrayList<Artikli>();
  369.  
  370. String myDriver="com.mysql.jdbc.Driver";
  371. String myUrl = "jdbc:mysql://localhost/eprodavnica2017";
  372. String mySQLuserNameString="root";
  373. String mySQLPasswordString="root";
  374.  
  375.  
  376. Connection connection;
  377.  
  378. Statement st;
  379. ResultSet rs;
  380.  
  381. try {
  382. connection = DriverManager.getConnection(myUrl, mySQLuserNameString, mySQLPasswordString);
  383.  
  384. st=connection.createStatement();
  385. rs=st.executeQuery(query);
  386.  
  387. while(rs.next()){
  388.  
  389. int id_artikal=rs.getInt("id_artikal");
  390. String naziv=rs.getString("naziv");
  391. String boja=rs.getString("boja");
  392. int kolicina_na_lageru=rs.getInt("kolicina_na_lageru");
  393. int cena=rs.getInt("cena");
  394. String naziv_prodavnice=rs.getString("naziv_prodavnice");
  395.  
  396. listaArtikala.add(new Artikli(id_artikal, naziv, boja, kolicina_na_lageru, cena, naziv_prodavnice));
  397.  
  398. }
  399.  
  400.  
  401.  
  402.  
  403.  
  404. } catch (SQLException ex) {
  405.  
  406. }
  407.  
  408. return listaArtikala;
  409.  
  410. }
  411.  
  412. public static void izmeniKolicinuArtikala(int idArtikla, int kolLager) {
  413.  
  414. String myDriver="com.mysql.jdbc.Driver";
  415. String myUrl = "jdbc:mysql://localhost/eprodavnica2017";
  416. String mySQLuserNameString="root";
  417. String mySQLPasswordString="root";
  418.  
  419.  
  420. Connection connection;
  421. Statement st;
  422. ResultSet rs;
  423.  
  424. try {
  425. connection = DriverManager.getConnection(myUrl, mySQLuserNameString, mySQLPasswordString);
  426.  
  427. st=connection.createStatement();
  428. rs=st.executeQuery("select * from artikli where id_artikal= '" + idArtikla + "';");
  429.  
  430. while(rs.next()){
  431.  
  432. //int id_artikal=rs.getInt("id_artikal");
  433.  
  434. String sql="update artikli set kolicina_na_lageru=? where id_artikal='" + idArtikla+ "';";
  435. PreparedStatement ps=connection.prepareStatement(sql);
  436. ps.setInt(1, kolLager);
  437. ps.executeUpdate();
  438.  
  439.  
  440. }
  441.  
  442.  
  443.  
  444.  
  445.  
  446. } catch (SQLException ex) {}
  447. }
  448.  
  449. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement