JSP, Servlets

K

KK

Hello,

I am trying to make a simple jsp and servlet application. I am using
J2EE1.4 server.
I have a login form in the jsp file and in its action i have mentioned
the name of the Servlet.
My jsp and html files are working properly but my servlets are not.
I have even tried Apache Tomcat..but situation remains same.

Can anyone suggest me anything?

Thanks
 
K

KK

When jsp page is forwarding to servlet page..its displaying a blank
page, if its not finding the servlet page then it should display
'Resource not found error' but it displays a blank page.
 
M

mirko.ebert

When jsp page is forwarding to servlet page..its displaying a blank
page, if its not finding the servlet page then it should display
'Resource not found error' but it displays a blank page.

Well, I think that is not the best Way to forward from a JSP to a
servlet. In my opinion, it's better to forward from a servlet to a
JSP. The the servlet works as a controller (MVC architecture). But
that's not the problem. Do you call the servelt via URL-pattern
(defined in the web.xml)? Can you post your web.xml? Can you also post
your forwarding code snippet.
 
K

KK

THIS is the form of the jsp page. Login is the name of the servlet
file.


<form name="form" method="post" action="Login">
<table border="0">

<font color="#5F512F">

<tr>
<td><strong><font color="#5F512F">Username</font></strong></
td><td><input type="text" name="j_username" size="15" maxlength="25"></
td></tr>


<tr>
<td><strong><font color="#5F512F">Password</font></strong></
td><td><input type="password" NAME="j_password" size="15"
maxlength="25"></td></tr>



</table>

</strong><center><BR>
<input type="Submit" value="Submit">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
type="Reset" value="Reset"></center>

</form>
 
K

KK

THIS is the XML file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>WebApp</display-name>
<servlet>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet>
<display-name>login</display-name>
<servlet-name>login</servlet-name>
<jsp-file>/login.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/Login</url-pattern>
</servlet-mapping>
</web-app>
 
K

KK

THIS is the servlet file.

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.http.*;

public class LoginServlet extends HttpServlet
{
public LoginServlet()
{

}
protected void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection c = null;
Statement st = null;
String username = req.getParameter("j_username");
System.out.println("<HTML>HELLO"+username+"</HTML>");
ResultSet rs= null;
System.out.println("Login Servlet");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:eek:dbc:pro","","");
st = c.createStatement();
rs = st.executeQuery("select * from login");
while(rs.next())
{
out.println(rs.getInt(1));
out.println(rs.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}





Its just a simple code, I wonder why its not displaying anything.
 
A

Arne Vajhøj

KK said:
THIS is the servlet file.

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.http.*;

public class LoginServlet extends HttpServlet
{
public LoginServlet()
{

}
protected void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection c = null;
Statement st = null;
String username = req.getParameter("j_username");
System.out.println("<HTML>HELLO"+username+"</HTML>");
ResultSet rs= null;
System.out.println("Login Servlet");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c = DriverManager.getConnection("jdbc:eek:dbc:pro","","");
st = c.createStatement();
rs = st.executeQuery("select * from login");
while(rs.next())
{
out.println(rs.getInt(1));
out.println(rs.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Its just a simple code, I wonder why its not displaying anything.

To debug you need to look at the console output.

If you get an exception (which is what I think you get) then you
do not get any output because you catch it.

And if there are no records in the table, then you do not get any
output.

Arne

PS: Note that your HTML output is send to the console not the
browser !!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top