Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- index.jsp
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body>
- <a href="unos.jsp">Unos</a><br/>
- <a href="ServletKola?ispis=lista">Lista</a><br/>
- <a href="ServletKola?ispis=jedan&br=1">Jedan</a><br/>
- </body>
- </html>
- -------
- indexLog.jsp
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>IndexLog</title>
- </head>
- <body>
- <form action="ServletLog" method="post">
- Ime:<input type="text" name="ime"><br/>
- Prezime:<input type="text" name="prezime"><br/>
- <input type="submit" value="Login"><br/>
- </form>
- <%
- String poruka = (String)request.getAttribute("poruka");
- if(poruka != null){
- %>
- <%=poruka%>
- <%}%>
- </body>
- </html>
- -----------------------
- kola.jsp
- <%@page import="car.Automobili"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body>
- <%
- Automobili a = (car.Automobili)request.getAttribute("kola");
- %>
- <table>
- <tr>
- <td>Marka" </td>
- <td>Cena: </td>
- <td>Brzina: </td>
- </tr>
- <tr>
- <td><%=a.getMarka()%></td>
- <td><%=a.getCena()%></td>
- <td><%=a.getBrzina()%></td>
- </tr>
- </table>
- </body>
- </html>
- ------------
- listaKola.jsp
- <%@page import="car.Automobili,java.util.ArrayList"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body>
- <%
- ArrayList<Automobili> au = (ArrayList<Automobili>)request.getAttribute("listaKola");
- %>
- <table>
- <tr>
- <td>Marka: </td>
- <td>Cena: </td>
- <td>Brzina: </td>
- </tr>
- <% for(Automobili pom:au){%>
- <tr>
- <td><%=pom.getMarka()%> </td>
- <td><%=pom.getCena()%></td>
- <td><%=pom.getBrzina()%></td>
- </tr>
- <%}%>
- </body>
- </html>
- ------------------
- unos.jsp
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Unos</title>
- </head>
- <body>
- <form action="ServletKola" method="POST">
- Marka: <input type="text" name="marka"><br/>
- Cena: <input type="text" name="cena"><br/>
- Brzina: <input type="text" name="brzina"><br/>
- <input type="submit" value="unos"><br/>
- </form>
- <%
- String poruka =(String)request.getAttribute("poruka");
- if( poruka != null){
- %>
- <%=poruka%>
- <%}%>
- </body>
- </html>
- ------------------------------------
- Automobili.java
- package car;
- public class Automobili {
- private String marka;
- private int cena;
- private int brzina;
- public Automobili() {
- }
- public Automobili(String marka, int cena, int brzina) {
- this.marka = marka;
- this.cena = cena;
- this.brzina = brzina;
- }
- public String getMarka() {
- return marka;
- }
- public void setMarka(String marka) {
- this.marka = marka;
- }
- public int getCena() {
- return cena;
- }
- public void setCena(int cena) {
- this.cena = cena;
- }
- public int getBrzina() {
- return brzina;
- }
- public void setBrzina(int brzina) {
- this.brzina = brzina;
- }
- }
- -------------
- ServletKola.java
- package car;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class ServletKola extends HttpServlet {
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- ArrayList<Automobili> au = new ArrayList<Automobili>();
- au.add(new Automobili("McLaren P1",100000,350));
- au.add(new Automobili("Lamborghini Aventador SV",10000,300));
- au.add(new Automobili("Mercedes-Benz S-Class Long AMG",1000000,350));
- String ispis = request.getParameter("ispis");
- if(ispis !=null){
- if(ispis.equals("lista")){
- request.setAttribute("listaKola", au);
- request.getRequestDispatcher("listaKola.jsp").forward(request, response);
- }
- else if(ispis.equals("jedan")){
- String br = request.getParameter("br");
- if(br !=null){
- request.setAttribute("kola",au.get(new Integer(br)));
- request.getRequestDispatcher("kola.jsp").forward(request, response);
- }
- }
- }else{
- request.setAttribute("poruka", "Nema sta da se izbaci");
- request.getRequestDispatcher("index.jsp").forward(request, response);
- }
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String marka = request.getParameter("marka");
- String cena = request.getParameter("cena");
- String brzina= request.getParameter("brzina");
- if(marka != null && cena != null && brzina != null){
- try{
- int Cena,Brzina;
- Cena = Integer.parseInt(cena);
- Brzina = Integer.parseInt(brzina);
- Automobili a = new Automobili(marka, Cena, Brzina);
- request.setAttribute("kola", a);
- request.getRequestDispatcher("kola.jsp").forward(request, response);
- }catch(Exception e){
- request.setAttribute("poruka", "Greska");
- request.getRequestDispatcher("unos.jsp").forward(request, response);
- }
- }else{
- request.setAttribute("poruka", "Morate popuniti sva polja");
- request.getRequestDispatcher("unos.jsp").forward(request, response);
- }
- }
- }
- ------------------
- ServletLog.java
- package car;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- 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 ServletLog extends HttpServlet {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- String ime = request.getParameter("ime");
- String prezime = request.getParameter("prezime");
- if( ime.isEmpty() || prezime.isEmpty()){
- request.setAttribute("poruka", "Morate popuniti sva polja");
- request.getRequestDispatcher("indexLog.jsp").forward(request, response);
- }
- try{
- Class.forName("com.mysql.jdbc.Driver");
- Connection kon = DriverManager.getConnection("jdbc:mysql://localhost:3306/korisnik","root","");
- PreparedStatement ps = kon.prepareStatement("select ime,prezime from korisnikinfo where ime=? and prezime= ?");
- ps.setString(1, ime);
- ps.setString(2, prezime);
- ResultSet rs = ps.executeQuery();
- if(rs.next()){
- response.sendRedirect("index.jsp");
- }else{
- request.setAttribute("poruka", "Morate popuniti sva polja");
- request.getRequestDispatcher("indexLog.jsp").forward(request, response);
- }
- kon.close();
- }catch(Exception e){
- }
- }
- }
- // <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