Advertisement
Guest User

Login Server test

a guest
Oct 1st, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.66 KB | None | 0 0
  1. //jsp login.jsp
  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  3.     pageEncoding="ISO-8859-1"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Insert title here</title>
  9. <script>
  10. function validate(){
  11.     var vUser = trim(document.frm.usr.value);
  12.     var vPwd = trim(document.frm.pwd.value);
  13.    
  14.     if(vUser == ""){
  15.         alert("Username field is empty!");
  16.         document.frm.usr.focus();
  17.         return false;
  18.     }else if(vPwd == ""){
  19.         alert("Password field is empty!");
  20.         document.frm.pwd.focus();
  21.         return false;
  22.     }
  23. }
  24.  
  25. function trim(s){
  26.    
  27.     return s.replace( /^s*/, "" ).replace( /s*$/, "");
  28. }
  29. </script>
  30. </head>
  31. <body>
  32. <form method="GET" action="login" name="frm" onSubmit="return validate();">
  33.   Username:<input type="text" name="usr"><br>
  34.   Password:<input type="password" name="pwd"><br>
  35.   <input type="submit" name="goTo" value="Login"/>
  36. </form>
  37. </body>
  38. </html>
  39.  
  40.  
  41. //LoginServlet.java (Servlet)
  42. package no.uia.login;
  43.  
  44. import java.io.IOException;
  45.  
  46. import javax.servlet.RequestDispatcher;
  47. import javax.servlet.ServletException;
  48. import javax.servlet.annotation.WebServlet;
  49. import javax.servlet.http.HttpServlet;
  50. import javax.servlet.http.HttpServletRequest;
  51. import javax.servlet.http.HttpServletResponse;
  52.  
  53. /**
  54.  * Servlet implementation class LoginServlet
  55.  */
  56. @WebServlet("/login")
  57. public class LoginServlet extends HttpServlet {
  58.     private static final long serialVersionUID = 1L;
  59.        
  60.     /**
  61.      * @see HttpServlet#HttpServlet()
  62.      */
  63.     public LoginServlet() {
  64.         super();
  65.         // TODO Auto-generated constructor stub
  66.     }
  67.  
  68.     /**
  69.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  70.      */
  71.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  72.         // TODO Auto-generated method stub
  73.        
  74.         String usr = request.getParameter("usr");
  75.         String pwd = request.getParameter("pwd");
  76.        
  77.         RequestDispatcher rd = null;
  78.        
  79.         rd = request.getRequestDispatcher("test.jsp");
  80.         rd.forward(request, response);
  81.     }
  82.  
  83.     /**
  84.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  85.      */
  86.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  87.         // TODO Auto-generated method stub
  88.     }
  89.  
  90. }
  91.  
  92.  
  93.  
  94. //test.jsp (Saves password as a session-variable)
  95. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  96.     pageEncoding="ISO-8859-1"%>
  97. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  98. <html>
  99. <head>
  100. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  101. <title>Insert title here</title>
  102. </head>
  103. <body>
  104. <%
  105. String usr = request.getParameter("usr");
  106. String pwd = request.getParameter("pwd");
  107. session.setAttribute("innlogget",pwd);
  108. %>
  109. <%=usr %>, du klarte det!
  110. <p><a href="resultat.jsp">Klikk her for å vise ditt hemmelige passord</a>)</p>
  111. </body>
  112. </html>
  113.  
  114.  
  115.  
  116. //resultat.jsp (Shows the session-variable (The password))
  117. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  118.     pageEncoding="ISO-8859-1"%>
  119. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  120. <%
  121. String pwd=(String)session.getAttribute("innlogget");
  122. if(pwd==null){
  123.     pwd="Passord ikke oppgitt";
  124. }
  125. %>
  126. <html>
  127. <head>
  128. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  129. <title>Innlogget</title>
  130. </head>
  131. <body>
  132. <p>Ditt hemmelige passord er: <%=pwd %></p>
  133. </body>
  134. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement