Jump to content

Cannot access to directed page


aristal

Recommended Posts

I was told to post my problem here. Can anyone offer me assistance?I have a servlet "Login.java" that is directed to different url depending on the login domain.The login jsp is in "Tomcat 5.5\webapps\FYP" folder and the directed pages are in the "Tomcat 5.5\webapps\FYP\WEB-INF\classes" folder.When i try to login using tomcat as the server, I get the following error."HTTP method GET is not supported by this URL.description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL)."The servlet code is as follow:import java.sql.*;import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*; public class Login extends HttpServlet{public void service (String gotoPage, HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException { //Get current session or create new oneHttpSession session = req.getSession(true); //Get login infoString Domain = req.getParameter("Domain");String username = req.getParameter("username");String password = req.getParameter("password"); PrintWriter out = res.getWriter(); try{System.out.println("\nEstablishing Connection - Pls Wait... \n"); //Connect to the database specified in the URLClass.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection con = DriverManager.getConnection("jdbc:odbc:DB1");System.out.println("Connection Established.\n"); //Create a SELECT statement objectStatement statmt = con.createStatement(); /*For COntractor*/if (Domain.equals("Contractor")){//Issue the SELECT statementString querySt = "SELECT Engineer_Name, FROM Organisation WHERE Engineer_Name = ? AND Password = ?"; // use PreparedStatement for more security and more easyPreparedStatement pstmt = con.prepareStatement(querySt); pstmt.setString(1, username); // set the 1st quesmark(?) icon value to the user input usernamepstmt.setString(2, password); // set the 2nd quesmark(?) icon value to the user input passwordResultSet rs = pstmt.executeQuery(querySt); if (rs.next()){// Got result mean username and password are correctgotoPage="/FYP/SupplierMainProj.jsp";//Retrieve each column in the row}else{// No result mean incorrect username and passwordgotoPage="/FYP/login.jsp";} // RequestDispatcher dispatcher =getServletContext().getRequestDispatcher(gotoPage);// dispatcher.forward(req, res); } /*For COntractor*/if (Domain.equals("Contractor")){//Issue the SELECT statementString querySt = "SELECT Project_Engineer, Password FROM Supplier WHERE Project_Engineer = ? AND Password = ?"; // use PreparedStatement for more security and more easyPreparedStatement pstmt = con.prepareStatement(querySt); pstmt.setString(1, username); // set the 1st quesmark(?) icon value to the user input usernamepstmt.setString(2, password); // set the 2nd quesmark(?) icon value to the user input passwordResultSet rs = pstmt.executeQuery(querySt); if (rs.next()){// Got result mean username and password are correctgotoPage="/FYP/Sub_main_proj.jsp";}else{// No result mean incorrect username and passwordgotoPage="/FYP/login.jsp";} RequestDispatcher dispatcher =getServletContext().getRequestDispatcher(gotoPage);dispatcher.forward(req, res);} //Close Statement and Connectionstatmt.close();con.close();System.out.println("\nConnection Closed - Operation Successful."); } catch(Exception E){//Print out the Exception ErrorSystem.out.println("Error:" +E );} } }this is my jsp code <HTML> <HEAD> <TITLE> Login </TITLE> <script language="JavaScript" type="text/JavaScript"><!--function MM_jumpMenu(targ,selObj,restore){ //v3.0 eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'"); if (restore) selObj.selectedIndex=0;}//--></script></HEAD> <BODY> <FORM ACTION="/FYP/servlet/Login" METHOD="doPost"> <CENTER><TABLE BORDER=0> <TR><TD COLSPAN=2><P Align=CENTER>Please enter the Name and<BR> password to login.<P Align=CENTER></TD></TR> <TR><TD><P Align=center> <table width="246" border="1" align="center"> <tr> <td width="75">User name</td> <td width="155"><input name="username" type="text" id="username"></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password"></td> </tr> <tr> <td>Domain</td> <td><select name="Domain" id="Domain" onChange="MM_jumpMenu('parent',this,0)"> <option value="Contractor" selected>Contractor</option> <option value="Subcontractor">Subcontractor</option> </select></td> </tr> </table> <p> </p></TD></TR><TR><TD><P></TD></TR> <TR><TD><P Align=CENTER><INPUT name="Submit" TYPE="submit" VALUE="Login"></TD></TR> </FORM></BODY></HTML>I have tried changing the "doGet" to "doPost" and service but the error merely change from "GET" to "POST". I have also tried removing the /FYP/ but it doesnt work either.Can anyone offer me some assistance? Your assistance is very much appreciated.Thanks.

Link to comment
Share on other sites

I have tried changing the "doGet" to "doPost" and service but the error merely change from "GET" to "POST". I have also tried removing the /FYP/ but it doesnt work either.
From what I remember - geez, it's been too long since I played with Java - you don't need to override the service method, you need a doGet (if you want to do GET requests) and a doPost (if you want to do POST requests).Try something simple first, to see if it works, and then add your database stuff later once you know it is working:
public class Login extends HttpServlet{	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException	{		response.setContentType("text/html");		PrintWriter out = response.getWriter();		out.println("<h1>HELLO</h1>");		out.close();	}}

EDIT: Oh, and the form method should either be "get" or "post" rather than "doGet" or "doPost":

<form action="/FYP/servlet/Login" method="get">

Link to comment
Share on other sites

Well, now that you know that doGet runs, you should just be able to start plugging the code that you had in your previous posting into that doGet method.

Link to comment
Share on other sites

Well, now that you know that doGet runs, you should just be able to start plugging the code that you had in your previous posting into that doGet method.
May i know what u mean by plugging? Does it mean that i have to install additional things?
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...