Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <%@ page import="data.StringConstants" %>
  5. <html>
  6. <head>
  7. <title>File Chooser</title>
  8. <link rel="stylesheet" href="${pageContext.request.contextPath}/css/main.css">
  9. <link href="https://fonts.googleapis.com/css?family=Lato:700i"
  10. rel="stylesheet">
  11. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  12. <script>
  13. $(document).ready(function(){
  14. $('#submit').click(function(event){
  15. console.log("Clicked");
  16. event.preventDefault();
  17. //alert('testing');
  18. var file = $("#fileName").val();
  19. console.log(file);
  20. var servlet = '${pageContext.request.contextPath}/FileChooserServlet';
  21. $.ajax({
  22. type: 'GET',
  23. url: servlet,
  24. data: {infile: file},
  25. success: function(data) {
  26. // check data for error
  27. console.log(data);
  28. if (data.includes("ERROR:")) {
  29. var errorMessage = data;
  30. document.getElementById('error').innerHTML = errorMessage;
  31. return false;
  32. } else {
  33. window.location = '${pageContext.request.contextPath}/jsp/login.jsp';
  34. }
  35. },
  36. error: function(data){
  37. alert("fail");
  38. }
  39. });
  40. });
  41. });
  42. </script>
  43. </head>
  44. <body>
  45. <div id="title_container">Cinemate</div>
  46.  
  47. <div id="welcome_text">
  48. Welcome to Cinemate, a Movie Social Media Medium.
  49. <br>
  50. Please input a file so that you may begin your experience.
  51. </div>
  52. <div id="outer_container">
  53. <div id="inner_container">
  54. <form name="filechooser">
  55. <input style = "width: 72%; margin-right: 5px;" type="text" id="fileName" name="<%= StringConstants.INFILE%>">
  56. <input style = "width: 20%;" type="submit" id = "submit" name="submit">
  57. <div class=error_message>
  58. <div id="error">
  59. <!-- if there is an error display it, else display the empty string -->
  60. </div>
  61. </div>
  62. </form>
  63. </div>
  64. </div>
  65. </body>
  66. </html>
  67.  
  68.  
  69.  
  70.  
  71. package servlets;
  72.  
  73. import java.io.IOException;
  74. import java.io.PrintWriter;
  75.  
  76. import javax.servlet.RequestDispatcher;
  77. import javax.servlet.ServletException;
  78. import javax.servlet.annotation.WebServlet;
  79. import javax.servlet.http.HttpServlet;
  80. import javax.servlet.http.HttpServletRequest;
  81. import javax.servlet.http.HttpServletResponse;
  82. import javax.servlet.http.HttpSession;
  83.  
  84. import data.DataStorage;
  85. import data.StringConstants;
  86.  
  87.  
  88. @WebServlet("/LoginServlet")
  89. public class LoginServlet extends HttpServlet {
  90. private static final long serialVersionUID = 1L;
  91.  
  92. public LoginServlet() {
  93. super();
  94. }
  95.  
  96. protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  97.  
  98. HttpSession session = request.getSession(true);
  99. DataStorage ds = (DataStorage) session.getAttribute(StringConstants.DATA);
  100. //get username and password
  101. String username = (String)request.getParameter(StringConstants.USERNAME);
  102. String password = (String)request.getParameter(StringConstants.PASSWORD);
  103. //if it is a valid username
  104. if (ds.validUsername(username)){
  105. //correct password
  106. if (ds.correctPassword(username, password)){
  107. ds.setLoggedInUser(username);
  108. response.sendRedirect(StringConstants.JSP_EXT+StringConstants.MENU_JSP);
  109. }
  110. //incorrect password
  111. else{
  112. request.setAttribute(StringConstants.ERROR, "Incorrect password");
  113. PrintWriter out = response.getWriter();
  114. out.print("ERROR:" + " Incorrect password");
  115. out.flush();
  116. //request.getRequestDispatcher(StringConstants.JSP_EXT+StringConstants.LOGIN_JSP).forward(request, response);
  117. }
  118. }
  119. //invalid username
  120. else{
  121. request.setAttribute(StringConstants.ERROR, "Invalid username");
  122. PrintWriter out = response.getWriter();
  123. out.print("ERROR:" + " Invalid username");
  124. out.flush();
  125. //request.getRequestDispatcher(StringConstants.JSP_EXT+StringConstants.LOGIN_JSP).forward(request, response);
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement