• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Servlet Query

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
I am new to servlets . My first program executed..
but the second one is having problem as all is going right but when the i submit the uid and pwd through the html i get a blank page.it shows nothing

Servlet Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class login extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
String uid=req.getParameter("t1");
String pwd=req.getParameter("t2");

PrintWriter out=res.getWriter();

res.setContentType("text/html");

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc dbc:ms","scott","tiger");

PreparedStatement psm=con.prepareStatement("select * from reg where uid1=? and pwd=?");
psm.setString(1,uid);
psm.setString(1,pwd);
ResultSet rs=psm.executeQuery();
if(rs.next())
{
out.println("<html><head><title>WELCOME</title></head>");
out.println("<body><b><i>Hello user "+uid+"</b></i>");
out.println("</body></html>");
}
else res.sendRedirect("http://localhost:8080/login.html");
}
catch(Exception e)
{
e.printStackTrace();}
}
}

plz help me out ...
thanks .
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would first verify that the parameters are populated before using them in a query.

String uid=req.getParameter("t1");
String pwd=req.getParameter("t2");

if(your uid and pw fields != null && their length is > 0)
do what ever

Also, thee is a chance that no rows were returned. You need to display something if no rows were found.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should also take the precaution of closing the output stream to force flushing of the contents. A simple out.close() will do the trick.
Bill
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any stack trace output in the console?
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic