java - 404 Requested Resource is not Available Eclipse/Tomcat -
this question has answer here:
i think i've read every question on topic far , tried lot of solutions, sorry if overlooked anything. i'm using eclipse tomcat 8. tomcat configured server , mysql-connector....jar in web-inf/lib folder, web.xml in /web-inf 1 , index in /webcontent
index.html:
<!doctype html> <html> <form action="servlets/start" method="post"> <font face="verdana" size="2"> enter table name :<input type="text" name="table"> <input type="submit" value="display"> </font> </form> </html
start.java:
import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; /** * servlet implementation class start */ @webservlet("/start") public class start extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public start() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse respond) throws servletexception, ioexception { printwriter pw=respond.getwriter(); respond.setcontenttype("text/html"); string tb=request.getparameter("table"); try { class.forname("oracle.jdbc.driver.oracledriver"); connection con=drivermanager.getconnection("jdbc::oracle::thin:@localhost:music","root","1234"); statement st=con.createstatement(); system.out.println("connection established successfully!"); resultset rs=st.executequery("select * from"+tb); pw.println("<table border=1>"); while(rs.next()) { pw.println("<tr><td>"+rs.getint(1)+"</td></td>+rs.getstring(2)+</td>"+"<td>"+rs.getstring(3)+"</td></tr>"); } pw.println("</table>"); pw.close(); } catch (exception e){ e.printstacktrace(); } // todo auto-generated method stub response.getwriter().append("served at: ").append(request.getcontextpath()); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); }
}
web.xml:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>servlets</display-name> <servlet> <servlet-name>start</servlet-name> <servlet-class>start.start</servlet-class> </servlet> <servlet-mapping> <servlet-name>start</servlet-name> <url-pattern>/start</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
now whenever run index.html on server, works fine, after pressing button specifies 404 error , have no idea why occurs. i've tried using /start action in index.html, leads error message pointing /start, when use servlets/start pointing servlets/servlets/start if helps. entering localhost:8080/servlets prompts me index.html leads same problem
as i'm getting frustrated, i'd ask help, in advance!
i have copied source codes , created web project in eclipse.
the start.java (inside package named start
) follows:-
package start; import java.io.ioexception; import java.io.printwriter; import java.sql.connection; import java.sql.drivermanager; import java.sql.resultset; import java.sql.statement; 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 start */ @webservlet("/start") public class start extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public start() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse * response) */ protected void doget(httpservletrequest request, httpservletresponse respond) throws servletexception, ioexception { printwriter pw = respond.getwriter(); respond.setcontenttype("text/html"); string tb = request.getparameter("table"); try { class.forname("oracle.jdbc.driver.oracledriver"); connection con = drivermanager.getconnection("jdbc::oracle::thin:@localhost:music", "root", "1234"); statement st = con.createstatement(); system.out.println("connection established successfully!"); resultset rs = st.executequery("select * from" + tb); pw.println("<table border=1>"); while (rs.next()) { pw.println("<tr><td>" + rs.getint(1) + "</td></td>+rs.getstring(2)+</td>" + "<td>" + rs.getstring(3) + "</td></tr>"); } pw.println("</table>"); pw.close(); } catch (exception e) { e.printstacktrace(); } // todo auto-generated method stub // response.getwriter().append("served at: // ").append(request.getcontextpath()); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse * response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
the web.xml has been modified remove servlet mapping exists @webservlet
on class itself. content follows:
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1"> <display-name>servlets</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
the index.html (in webcontent folder) follows:
<!doctype html> <html> <form action="start" method="post"> <font face="verdana" size="2"> enter table name :<input type="text" name="table"> <input type="submit" value="display"> </font> </form> </html>
the result can access web application @ http://localhost:8080/servlets/
, clicking on display button, go next page well.
Comments
Post a Comment