[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Convert the login page to jsp

Posted on 2004-03-24
2
Medium Priority
?
513 Views
Last Modified: 2013-11-23
Can someone help me convert the program below to jsp?

Program:

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class LoginServlet extends HttpServlet {

  public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {sendLoginForm(response, false);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

        String password = request.getParameter("password");
        String userName = request.getParameter("staffid");
      //String adminName = request.getParameter("administrator");

     if (login(userName, password)){

              HttpSession session = request.getSession();
              String event = request.getParameter("staffid");
              if (event.equals("administrator")) {
                     response.sendRedirect("../,,/client/adminmainpage.jsp");
              } else {
                          response.sendRedirect("./../client/mainpage.jsp");
              }

      }

        else {
              sendLoginForm(response, true);
         }

     }
  private void sendLoginForm(HttpServletResponse response,boolean withErrorMessage)throws ServletException, IOException {

  response.setContentType("text/html");
  PrintWriter out= response.getWriter();
  out.println("<HTML>");
  out.println("<HEAD>");
  out.println("<TITLE>Login</TITLE>");
  out.println("</HEAD>");
  out.println("<body background=background.jpg style=background-image: url(../../client/images/background.jpg)><h1>");
  out.println("<CENTER>");

  if (withErrorMessage)
  out.println("Login failed. Please try again.<BR>");
   out.println("<h1><img src="+"../../client/images/header.gif"+">");
   out.println("</b></font></h1>");
   out.println("<table border=4 cellpadding=10 cellspacing=0 align=center bordercolor=#333399 bordercolordark=black bordercolorlight=#333399 width=409>");
   out.println("<tr><td width=379 bgcolor=#E6E6E6>");
   out.println("<table border=0 cellpadding=5 cellspacing=0 align="+"center"+">");
   out.println("<tr><td align=center><h3>");
   out.println("<font color="+"#0033CC"+">");
   out.println("<FORM METHOD=POST>");
   out.println("<font size=3>To log in enter your Staff ID and Password.</font></font><font color=#0000FF size=3>");
   out.println("</h3></td></tr></table>");
   out.println("<table border=0 cellpadding=5 cellspacing=0 align="+"center"+">");
   out.println("<tr><td><B> <font color=#333333> Staff ID</font></B></td>");
   out.println("<td>:</td>");
   out.println("<td><input type=text name=staffid </td></tr>");
   out.println("<tr><td><B>Password</B></td>");
   out.println("<td>:</td>");
   out.println("<td><input type=password name=password size=20>"+"                </table><table border=0 cellpadding=5 cellspacing=0 align="+"center"+">");
   out.println("<tr><td align=center><input type=submit Name=\"login\" value=\"     OK      \">"+"&nbsp;&nbsp;&nbsp;&nbsp;"+"<input type=reset value=\"Cancel\">"+"</td></tr>");
   out.println("</table></td></tr></table>");
   out.println("<center><font size=2 color=#A52A2A>");
   out.println("<br><marquee align=middle width=450 height=19><B>"
                    +"! Thank You For Doing the Feedback.</B></marquee>");
   out.println("</font></center></strong></BODY></HTML>");
}



 boolean login(String userName, String password) {
       try{
             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
             Connection con =DriverManager.getConnection("jdbc:odbc:StudFdBk");
             System.out.println("got connection");
         System.out.println("Hello "+ userName);
         System.out.println("Hello "+ password);
             Statement s = con.createStatement();
             String sql = "SELECT STAFFID, PASSWORD FROM Staff " +
                          "WHERE StaffID = '"+ userName + "' "+
                          "AND Password = '"+ password + "' ";
             ResultSet rs = s.executeQuery(sql);
             if (rs.next()) {
                   rs.close();
                   s.close();
                   con.close();
                   return true;
             }

             rs.close();
             s.close();
             con.close();
       }
       catch (ClassNotFoundException e) {
              System.out.println(e.toString());
       }
       catch (SQLException e) {
              System.out.println(e.toString());
       }
       catch(Exception e){
             System.out.println(e.toString());
       }
       return false;
  }
}
0
Comment
Question by:Smiley31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 300 total points
ID: 10674569
First off, why do you want to do that? The result would be more or less the same.

Secondly, the JSP would be ultimately compiled into a Servlet only.

Still, if you want to do it, then you can do it:

- Remove the class/ methods.

- Simply use the tags <HTML>, etc instead of out.println ( "HTML" ), etc

- wherever some Java logic is required, enclose the Java code in <% %>
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10720802
Why a B :-(

You could've asked for more assistance if you needed it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question