Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @WebServlet(name = "SignInServlet", urlPatterns = {"/SignInServlet"})
- public class SignInServlet extends HttpServlet
- {
- User user;
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException
- {
- user = new User();
- String login = req.getParameter("login");
- String password = req.getParameter("password");
- String role;
- resp.setContentType("text/html;charset=utf-8");
- PrintWriter pw = resp.getWriter();
- Connection conn = null;
- String url = "jdbc:postgresql://127.0.0.1:5432/crm";
- String name = "vladimir";
- String pass = "1234";
- try
- {
- Class.forName("org.postgresql.Driver");
- conn = DriverManager.getConnection(url, name, pass);
- Statement statement = conn.createStatement();
- ResultSet result1 = statement.executeQuery("SELECT * FROM users");
- while (result1.next())
- {
- if((password.equals(result1.getString("password"))) &&
- (login.equals(result1.getString("login"))))
- {
- user.setID(result1.getInt("iduser"));
- user.setLogin(result1.getString("login"));
- user.setUserrole(result1.getString("userrole"));
- req.setAttribute("login", login);
- req.getRequestDispatcher("/signiinsucces.jsp").
- forward(req, resp);
- }
- /*else if((!password.equals(result1.getString("password"))) &&
- (!login.equals(result1.getString("login"))))
- {
- getServletContext().
- getRequestDispatcher("/signiinfail.jsp").
- forward(req, resp);
- }*/
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- getServletContext().
- getRequestDispatcher("/error.jsp").
- forward(req, resp);
- }
- finally
- {
- if (conn != null)
- {
- try
- {
- conn.close();
- }
- catch (SQLException ex)
- {
- getServletContext().
- getRequestDispatcher("/error.jsp").
- forward(req, resp);
- }
- }
- }
- }
- }
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Вход</title>
- </head>
- <body>
- <form action="SignInServlet">
- <p>
- Ваш логин<br>
- <input type="text" size="40" max="30" min="1" name="login">
- </p>
- <p>
- Ваш пароль<br>
- <input type="password" size="40" max="25" min="1"
- name="password">
- </p>
- <p>
- <input type="submit" value="Войти">
- </p>
- </form>
- <br><br>
- <p>
- <a href="index.jsp">На главную страницу</a>
- </p>
- </body>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Начальная страница создания сайта</title>
- </head>
- <body>
- <form action="SiteServlet">
- <p>
- Название сайта<br>
- <input type="text" size="40" max="30" min="1" name="sitename">
- </p>
- <p>
- <input type="submit" value="Отправить">
- </p>
- </form>
- <br><br>
- <p>
- <a href="signiinsucces.jsp">Назад</a>
- </p>
- </body>
- @WebServlet(name = "SiteServlet", urlPatterns = {"/SiteServlet"})
- public class SiteServlet extends HttpServlet
- {
- User user;
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException
- {
- try
- {
- user = new User();
- String sitename = req.getParameter("sitename");
- int id = user.getID();
- String login = user.getLogin();
- resp.setContentType("text/html;charset=utf-8");
- PrintWriter pw = resp.getWriter();
- Connection conn = null;
- String url = "jdbc:postgresql://127.0.0.1:5432/crm";
- String name = "vladimir";
- String pass = "1234";
- try
- {
- Class.forName("org.postgresql.Driver");
- conn = DriverManager.getConnection(url, name, pass);
- Statement statement = conn.createStatement();
- //JOptionPane.showMessageDialog(null,conn.toString());
- if(id != 0)
- {
- String insert = "insert into site values(" + id +
- ", '" + login + "', '" + sitename + "')";
- statement.executeUpdate(insert);
- req.getRequestDispatcher("/template.jsp").forward(req, resp);
- }
- else
- {
- req.getRequestDispatcher("/error2.jsp").forward(req, resp);
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- getServletContext().
- getRequestDispatcher("/error.jsp").
- forward(req, resp);
- }
- finally
- {
- if (conn != null)
- {
- try
- {
- conn.close();
- }
- catch (SQLException ex)
- {
- getServletContext().
- getRequestDispatcher("/error.jsp").
- forward(req, resp);
- }
- }
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- httpSession.setAttribute("id", id)
- httpSession.setAttribute("login", login)
Add Comment
Please, Sign In to add comment