Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab3;
- import javax.faces.application.FacesMessage;
- import javax.faces.component.UIComponent;
- import javax.faces.component.html.HtmlInputSecret;
- import javax.faces.component.html.HtmlInputText;
- import javax.faces.context.FacesContext;
- import javax.faces.validator.Validator;
- import javax.faces.validator.ValidatorException;
- import org.apache.commons.lang3.StringUtils;
- public class AlphanumericValidator implements Validator {
- @Override
- public void validate(FacesContext context, UIComponent uiComp, Object value) throws ValidatorException {
- org.apache.commons.validator.EmailValidator emailValidator = org.apache.commons.validator.EmailValidator.getInstance();
- if(!StringUtils.isAlphaSpace((String) value))
- {
- HtmlInputSecret htmlInputSecret = (HtmlInputSecret) uiComp;
- FacesMessage facesMessage = new FacesMessage(htmlInputSecret.getLabel()
- + ": dozwolone tylko litery");
- throw new ValidatorException(facesMessage);
- }
- }
- }
- package lab3;
- import com.j256.ormlite.field.DatabaseField;
- import com.j256.ormlite.table.DatabaseTable;
- @DatabaseTable(tableName="IMIENINY")
- public class AM_Imieniny {
- @DatabaseField(generatedId = true)
- private int id;
- @DatabaseField(columnName = "IMIE")
- private String imie;
- @DatabaseField(columnName = "MIESIAC")
- private int miesiac;
- @DatabaseField(columnName = "DZIEN")
- private int dzien;
- public AM_Imieniny() {
- }
- public AM_Imieniny(String imie, int miesiac, int dzien) {
- super();
- this.imie = imie;
- this.miesiac = miesiac;
- this.dzien = dzien;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getImie() {
- return imie;
- }
- public void setImie(String imie) {
- this.imie = imie;
- }
- public int getMiesiac() {
- return miesiac;
- }
- public void setMiesiac(int miesiac) {
- this.miesiac = miesiac;
- }
- public int getDzien() {
- return dzien;
- }
- public void setDzien(int dzien) {
- this.dzien = dzien;
- }
- @Override
- public String toString() {
- return "AM_Imieniny [id=" + id + ", imie=" + imie + ", miesiac=" + miesiac + ", dzien=" + dzien + "]";
- }
- }
- package lab3;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.sqlite.SQLiteException;
- import com.j256.ormlite.dao.Dao;
- import com.j256.ormlite.dao.DaoManager;
- import com.j256.ormlite.dao.GenericRawResults;
- import com.j256.ormlite.jdbc.JdbcConnectionSource;
- import com.j256.ormlite.stmt.RawResultsImpl;
- import com.j256.ormlite.support.ConnectionSource;
- import com.j256.ormlite.table.TableUtils;
- /**
- * Servlet implementation class OrmApp
- */
- @WebServlet("/Imieniny")
- public class AM_ImieninyApp extends HttpServlet {
- private static final long serialVersionUID = 1L;
- String databaseUrl = "jdbc:sqlite:imieniny.sqlite";
- public AM_ImieninyApp() {
- super();
- }
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("text/plain; charset=utf-8");
- response.setCharacterEncoding("utf-8");
- PrintWriter out = response.getWriter();
- // try {
- String imie = request.getParameter("imie");
- if(imie == null || imie.equals(""))
- {
- List<String> imiona = getWszystkieImiona();
- for(String x : imiona)
- {
- out.println(x);
- }
- }
- else
- {
- List<AM_Imieniny> lista = getImieniny(imie);
- if(lista == null)
- {
- out.println("Brak imienin dla imienia " + imie);
- return;
- }
- for(AM_Imieniny x : lista)
- {
- out.println(x.getImie() + ", " + x.getDzien() + ", " + x.getMiesiac());
- }
- }
- // List<Ksiazka> listaKsiazek = ksiazkaDao.queryForAll();
- // out.println("ksiazek jest " + listaKsiazek.size());
- // connectionSource.close();
- // } catch (SQLException e) {
- // e.printStackTrace();
- // }
- }
- private List<String> getWszystkieImiona() {
- try {
- ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
- Dao<AM_Imieniny, Integer> imieninyDao = DaoManager.createDao(connectionSource, AM_Imieniny.class);
- GenericRawResults<String[]> rawResults = imieninyDao.queryRaw("SELECT imie FROM IMIENINY");
- List<String[]> wynik = rawResults.getResults();
- List<String> lista = new ArrayList<>();
- for(String[] x : wynik)
- {
- lista.add(x[0]);
- //out.println(Arrays.toString(x));
- }
- return lista;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- private List<AM_Imieniny> getImieniny(String imie)
- {
- try {
- ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
- Dao<AM_Imieniny, Integer> imieninyDao = DaoManager.createDao(connectionSource, AM_Imieniny.class);
- GenericRawResults<String[]> rawResults = imieninyDao.queryRaw("SELECT * FROM IMIENINY");
- List<String[]> wynik = rawResults.getResults();
- List<AM_Imieniny> lista = new ArrayList<>();
- for(String[] x : wynik)
- {
- if(x[1].startsWith(imie))
- lista.add(new AM_Imieniny(x[1], Integer.parseInt(x[2]), Integer.parseInt(x[3])));
- //out.println(Arrays.toString(x));
- }
- return lista;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- }
- package lab3;
- import javax.faces.application.FacesMessage;
- import javax.faces.component.UIComponent;
- import javax.faces.component.html.HtmlCommandButton;
- import javax.faces.component.html.HtmlInputSecret;
- import javax.faces.component.html.HtmlSelectBooleanCheckbox;
- import javax.faces.context.FacesContext;
- import javax.faces.event.ValueChangeEvent;
- import javax.faces.validator.ValidatorException;
- import org.apache.commons.lang3.StringUtils;
- public class ImieninyBean {
- private String username;
- private String password;
- private HtmlCommandButton submitButton;
- // private HtmlSelectBooleanCheckbox selectBooleanCheckbox1;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String register() {
- return (username.equals("user@user.com") && password.equals("user")) ? "success" : "failure";
- }
- public String login() {
- return (username.equals("user@user.com") && password.equals("user")) ? "success" : "failure";
- }
- public HtmlCommandButton getSubmitButton() {
- return submitButton;
- }
- public void setSubmitButton(HtmlCommandButton submitButton) {
- this.submitButton = submitButton;
- }
- // public HtmlSelectBooleanCheckbox getSelectBooleanCheckbox1() {
- // return selectBooleanCheckbox1;
- // }
- // public void setSelectBooleanCheckbox1(HtmlSelectBooleanCheckbox selectBooleanCheckbox1) {
- // this.selectBooleanCheckbox1 = selectBooleanCheckbox1;
- // }
- //
- // public void checkbox1Changed(ValueChangeEvent valueChangedEvent)
- // {
- // if(selectBooleanCheckbox1.isSelected())
- // {
- // submitButton.setDisabled(false);
- // } else {
- // submitButton.setDisabled(true);
- // }
- //
- // FacesContext context = FacesContext.getCurrentInstance();
- // context.renderResponse();
- // }
- public void validateAlpha(FacesContext context, UIComponent uiComp, Object value)
- throws ValidatorException
- {
- if(!StringUtils.isAlphaSpace((String) value))
- {
- HtmlInputSecret htmlInputSecret = (HtmlInputSecret) uiComp;
- FacesMessage facesMessage = new FacesMessage(htmlInputSecret.getLabel()
- + ": dozwolone tylko litery");
- throw new ValidatorException(facesMessage);
- }
- }
- }
- package lab3;
- import java.io.IOException;
- import java.sql.SQLException;
- import java.util.List;
- import javax.faces.application.FacesMessage;
- import javax.faces.component.UIComponent;
- import javax.faces.component.html.HtmlCommandButton;
- import javax.faces.component.html.HtmlInputSecret;
- import javax.faces.component.html.HtmlInputText;
- import javax.faces.component.html.HtmlSelectBooleanCheckbox;
- import javax.faces.context.FacesContext;
- import javax.faces.event.ValueChangeEvent;
- import javax.faces.validator.ValidatorException;
- import org.apache.commons.lang3.StringUtils;
- import com.j256.ormlite.dao.Dao;
- import com.j256.ormlite.dao.DaoManager;
- import com.j256.ormlite.dao.GenericRawResults;
- import com.j256.ormlite.jdbc.JdbcConnectionSource;
- import com.j256.ormlite.support.ConnectionSource;
- import com.j256.ormlite.table.TableUtils;
- public class Login {
- private String username;
- private String password;
- private HtmlCommandButton submitButton;
- // private HtmlSelectBooleanCheckbox selectBooleanCheckbox1;
- private static final long serialVersionUID = 1L;
- static String databaseUrl = "jdbc:sqlite:ludzie.sqlite";
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- //
- // public String register() {
- //
- // return (username.equals("user@user.com") && password.equals("user")) ? "success" : "failure";
- // }
- public class Db
- {
- boolean login(String name, String password)
- {
- System.out.println("name = " + name ==null ? "null" : name);
- System.out.println("password = " + password ==null ? "null" : password);
- try {
- ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
- Dao<User, Integer> userDao = DaoManager.createDao(connectionSource, User.class);
- TableUtils.createTableIfNotExists(connectionSource, User.class);
- User user = new User(name, password);
- GenericRawResults<String[]> rawResults = userDao.queryRaw("SELECT * FROM ludzie");
- List<String[]> wynik = rawResults.getResults();
- for(String[] x : wynik)
- {
- if(x[1].equals(name) && x[2].equals(password))
- {
- return true;
- }
- }
- try {
- connectionSource.close();
- } catch (IOException e) {
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- return false;
- }
- }
- public String login() {
- Db db = new Db();
- System.out.println("name = " + username ==null ? "null" : username);
- System.out.println("password = " + password ==null ? "null" : password);
- return db.login(username, password) ? "success" : "failure";
- }
- public HtmlCommandButton getSubmitButton() {
- return submitButton;
- }
- public void setSubmitButton(HtmlCommandButton submitButton) {
- this.submitButton = submitButton;
- }
- // public HtmlSelectBooleanCheckbox getSelectBooleanCheckbox1() {
- // return selectBooleanCheckbox1;
- // }
- // public void setSelectBooleanCheckbox1(HtmlSelectBooleanCheckbox selectBooleanCheckbox1) {
- // this.selectBooleanCheckbox1 = selectBooleanCheckbox1;
- // }
- //
- // public void checkbox1Changed(ValueChangeEvent valueChangedEvent)
- // {
- // if(selectBooleanCheckbox1.isSelected())
- // {
- // submitButton.setDisabled(false);
- // } else {
- // submitButton.setDisabled(true);
- // }
- //
- // FacesContext context = FacesContext.getCurrentInstance();
- // context.renderResponse();
- // }
- public void validateEmail(FacesContext context, UIComponent uiComp, Object value)
- throws ValidatorException
- {
- org.apache.commons.validator.EmailValidator emailValidator = org.apache.commons.validator.EmailValidator.getInstance();
- if(!emailValidator.isValid((String) value))
- {
- HtmlInputText htmlInputSecret = (HtmlInputText) uiComp;
- FacesMessage facesMessage = new FacesMessage(htmlInputSecret.getLabel()
- + ": Email bledny");
- throw new ValidatorException(facesMessage);
- }
- }
- }
- package lab3;
- import java.io.IOException;
- import java.sql.SQLException;
- import javax.faces.application.FacesMessage;
- import javax.faces.component.UIComponent;
- import javax.faces.component.html.HtmlCommandButton;
- import javax.faces.component.html.HtmlInputSecret;
- import javax.faces.component.html.HtmlInputText;
- import javax.faces.component.html.HtmlSelectBooleanCheckbox;
- import javax.faces.context.FacesContext;
- import javax.faces.event.ValueChangeEvent;
- import javax.faces.validator.ValidatorException;
- import org.apache.commons.lang3.StringUtils;
- import com.j256.ormlite.dao.Dao;
- import com.j256.ormlite.dao.DaoManager;
- import com.j256.ormlite.jdbc.JdbcConnectionSource;
- import com.j256.ormlite.support.ConnectionSource;
- import com.j256.ormlite.table.TableUtils;
- public class Register {
- private String username;
- private String password;
- private static final long serialVersionUID = 1L;
- static String databaseUrl = "jdbc:sqlite:ludzie.sqlite";
- static
- {
- // workaround aby dropowac tylko raz
- try {
- ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
- Dao<User, Integer> userDao = DaoManager.createDao(connectionSource, User.class);
- TableUtils.dropTable(connectionSource, User.class, true);
- TableUtils.createTableIfNotExists(connectionSource, User.class);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private HtmlCommandButton submitButton;
- // private HtmlSelectBooleanCheckbox selectBooleanCheckbox1;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- // public String register() {
- //
- // return (username.equals("user@user.com") && password.equals("user")) ? "success" : "failure";
- // }
- public class Db
- {
- boolean register(String name, String password)
- {
- try {
- ConnectionSource connectionSource = new JdbcConnectionSource(databaseUrl);
- Dao<User, Integer> userDao = DaoManager.createDao(connectionSource, User.class);
- TableUtils.createTableIfNotExists(connectionSource, User.class);
- User user = new User(name, password);
- userDao.create(user);
- try {
- connectionSource.close();
- } catch (IOException e) {
- }
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- }
- public String register() {
- Db db = new Db();
- return db.register(username, password) ? "success" : "failure";
- }
- public HtmlCommandButton getSubmitButton() {
- return submitButton;
- }
- public void setSubmitButton(HtmlCommandButton submitButton) {
- this.submitButton = submitButton;
- }
- // public HtmlSelectBooleanCheckbox getSelectBooleanCheckbox1() {
- // return selectBooleanCheckbox1;
- // }
- // public void setSelectBooleanCheckbox1(HtmlSelectBooleanCheckbox selectBooleanCheckbox1) {
- // this.selectBooleanCheckbox1 = selectBooleanCheckbox1;
- // }
- //
- // public void checkbox1Changed(ValueChangeEvent valueChangedEvent)
- // {
- // if(selectBooleanCheckbox1.isSelected())
- // {
- // submitButton.setDisabled(false);
- // } else {
- // submitButton.setDisabled(true);
- // }
- //
- // FacesContext context = FacesContext.getCurrentInstance();
- // context.renderResponse();
- // }
- public void validateEmail(FacesContext context, UIComponent uiComp, Object value)
- throws ValidatorException
- {
- org.apache.commons.validator.EmailValidator emailValidator = org.apache.commons.validator.EmailValidator.getInstance();
- if(!emailValidator.isValid((String) value))
- {
- HtmlInputText htmlInputSecret = (HtmlInputText) uiComp;
- FacesMessage facesMessage = new FacesMessage(htmlInputSecret.getLabel()
- + ": Email bledny");
- throw new ValidatorException(facesMessage);
- }
- }
- }
- package lab3;
- import com.j256.ormlite.field.DatabaseField;
- import com.j256.ormlite.table.DatabaseTable;
- @DatabaseTable(tableName="ludzie")
- public class User {
- @DatabaseField(generatedId = true)
- private int id;
- @DatabaseField(columnName = "email")
- private String email;
- @DatabaseField(columnName = "haslo")
- private String haslo;
- public User() {
- }
- public User(String email, String haslo) {
- super();
- this.email = email;
- this.haslo = haslo;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public String getHaslo() {
- return haslo;
- }
- public void setHaslo(String haslo) {
- this.haslo = haslo;
- }
- @Override
- public String toString() {
- return "User [id=" + id + ", email=" + email + ", haslo=" + haslo + "]";
- }
- }
- //JSP
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
- <!DOCTYPE html>
- <f:view>
- <html>
- <head>
- <meta charset="utf-8">
- <title>ERROR</title>
- </head>
- <body>
- <h1>Niestety, nie udało się wykonac operacji!</h1>
- </body>
- </html>
- </f:view>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Sugestie Lab1</title>
- <script type="text/javascript">
- function pobierzSugestie() {
- tekst = document.getElementById("pole").value;
- if(tekst != "") {
- requester = new XMLHttpRequest();
- requester.onreadystatechange = myHandler;
- requester.open("POST", "Sugestie");
- requester.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
- requester.send("query=" + tekst);
- }
- }
- function myHandler() {
- if(requester.readyState == 4) {
- if(requester.status == 200) {
- el = document.getElementById("lista");
- lista = requester.responseText.split(";");
- odpowiedz = "";
- for(i=0; i < lista.length - 1; ++i) {
- odpowiedz += (lista[i]+"<br/>");
- }
- el.innerHTML = odpowiedz;
- }
- }
- }
- </script>
- </head>
- <body>
- <h4>Wpisz marke samochodu</h4>
- <input id="pole" type="text" onkeyup="javascript:pobierzSugestie();" />
- <div id="lista"></div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
- <!DOCTYPE html>
- <f:view>
- <html>
- <head>
- <meta charset="utf-8">
- <title>IMIENINY!</title>
- </head>
- <body>
- <h:form>
- <h1>ZALOGUJ</h1>
- <p>Uzytkownik: <h:inputText id="username"
- value="#{loginBean.username}"
- label="Uzytkownik"
- required="true"
- validator="#{loginBean.validateEmail}"/>
- <h:message for="username"/>
- </p>
- <p>Hasło: <h:inputSecret
- id="password"
- value="#{loginBean.password}"
- label="Haslo"
- required="true">
- <f:validator validatorId="alphaValidator"/>
- </h:inputSecret>
- <h:message for="password"/>
- </p>
- <p><h:commandButton value="Zaloguj"
- id="submitButton"
- action="#{loginBean.login}"
- binding="#{loginBean.submitButton}"
- disabled="false"/>
- </p>
- </h:form>
- </body>
- </html>
- </f:view>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
- <!DOCTYPE html>
- <f:view>
- <html>
- <head>
- <meta charset="utf-8">
- <title>OK</title>
- </head>
- <body>
- <p>Zostałeś zalogowany z emailem: <h:outputText value="#{loginBean.username}!"/></p>
- <p>Twoje haslo to: <h:outputText value="#{loginBean.password}!"/></p>
- <br/>
- </body>
- </html>
- </f:view>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
- <!DOCTYPE html>
- <f:view>
- <html>
- <head>
- <meta charset="utf-8">
- <title>OK</title>
- </head>
- <body>
- <p>Zostałeś zarejestrowany z mailem <h:outputText value="#{loginBean.username}!"/></p>
- <!-- <p>Twoje haslo to: <h:outputText value="#{loginBean.password}!"/></p>
- --><p>
- </body>
- </html>
- </f:view>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
- <!DOCTYPE html>
- <f:view>
- <html>
- <head>
- <meta charset="utf-8">
- <title>IMIENINY!</title>
- </head>
- <body>
- <h:form>
- <p>Uzytkownik: <h:inputText id="username"
- value="#{registerBean.username}"
- label="Uzytkownik"
- required="true"
- validator="#{registerBean.validateEmail}"/>
- <h:message for="username"/>
- </p>
- <p>Hasło: <h:inputSecret
- id="password"
- value="#{registerBean.password}"
- label="Haslo"
- required="true">
- <f:validator validatorId="alphaValidator"/>
- </h:inputSecret>
- <h:message for="password"/>
- </p>
- <p><h:commandButton value="Zarejestruj"
- id="submitButton"
- action="#{registerBean.register}"
- binding="#{registerBean.submitButton}"
- disabled="false"/>
- </p>
- </h:form>
- </body>
- </html>
- </f:view>
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="utf-8"%>
- <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
- <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
- <!DOCTYPE html>
- <f:view>
- <html>
- <head>
- <meta charset="utf-8">
- <title>IMIENINY!</title>
- </head>
- <body>
- <h:form>
- <h1>ZALOGOWANY</h1>
- <p>Zostałeś zarejestrowany z mailem <h:outputText value="#{loginBean.username}!"/></p>
- <p>Twoje haslo to: <h:outputText value="#{loginBean.password}!"/></p>
- Imieniny:
- <br/>
- <input type="text" name="imie"><br>
- <p><h:commandButton value="Sprawdz imieniny [jeszcze nie zaimplementowane :(]"
- id="submitButton"
- disabled="true"/>
- </p>
- </h:form>
- </body>
- </html>
- </f:view>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement