Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //jsp login.jsp
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- <script>
- function validate(){
- var vUser = trim(document.frm.usr.value);
- var vPwd = trim(document.frm.pwd.value);
- if(vUser == ""){
- alert("Username field is empty!");
- document.frm.usr.focus();
- return false;
- }else if(vPwd == ""){
- alert("Password field is empty!");
- document.frm.pwd.focus();
- return false;
- }
- }
- function trim(s){
- return s.replace( /^s*/, "" ).replace( /s*$/, "");
- }
- </script>
- </head>
- <body>
- <form method="GET" action="login" name="frm" onSubmit="return validate();">
- Username:<input type="text" name="usr"><br>
- Password:<input type="password" name="pwd"><br>
- <input type="submit" name="goTo" value="Login"/>
- </form>
- </body>
- </html>
- //LoginServlet.java (Servlet)
- package no.uia.login;
- import java.io.IOException;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- * Servlet implementation class LoginServlet
- */
- @WebServlet("/login")
- public class LoginServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public LoginServlet() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- String usr = request.getParameter("usr");
- String pwd = request.getParameter("pwd");
- RequestDispatcher rd = null;
- rd = request.getRequestDispatcher("test.jsp");
- rd.forward(request, response);
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
- */
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- }
- }
- //test.jsp (Saves password as a session-variable)
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Insert title here</title>
- </head>
- <body>
- <%
- String usr = request.getParameter("usr");
- String pwd = request.getParameter("pwd");
- session.setAttribute("innlogget",pwd);
- %>
- <%=usr %>, du klarte det!
- <p><a href="resultat.jsp">Klikk her for å vise ditt hemmelige passord</a>)</p>
- </body>
- </html>
- //resultat.jsp (Shows the session-variable (The password))
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <%
- String pwd=(String)session.getAttribute("innlogget");
- if(pwd==null){
- pwd="Passord ikke oppgitt";
- }
- %>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Innlogget</title>
- </head>
- <body>
- <p>Ditt hemmelige passord er: <%=pwd %></p>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement