nagarjuna
servelets
1 Answer(s)      3 years and 3 months ago
Posted in : Java Beginners

I want to develop a small web application using netbeans.It contains a user login page,after sign into the page it will shows a new panel.


Plese send me links which contains theory and practical information.
View Answers

February 9, 2010 at 1:29 PM


Hi Friend,

Try the following code:

1)login.jsp:

<html>
<head>
</head>
<body>
<form name="form" method="post" action="../Authenticate">
<br><br>
<table align="center"><tr><td><h2>Login Authentication</h2></td></tr></table>
<table width="300px" align="center" style="border:1px solid #000000;background-color:#efefef;">
<tr><td colspan=2></td></tr>
<tr><td colspan=2>&nbsp;</td></tr>
<tr>
<td><b>Login Name</b></td>
<td><input type="text" name="username" value=""></td>
</tr>
<tr>
<td><b>Password</b></td>
<td><input type="password" name="password" value=""></td>
</tr>

<tr>
<td></td>
<td><input type="submit" name="Submit" value="Submit"></td>
</tr>
<tr><td colspan=2>&nbsp;</td></tr>
</table>
</form>
</body>
</html>

2)Authenticate.java:

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

public class Authenticate extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
PrintWriter out = response.getWriter();
String user=request.getParameter("username");
String pass=request.getParameter("password");
String user1="";
String pass1="";
HttpSession session = request.getSession(true);
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root";);
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
while(rs.next()){
user1 = rs.getString("username");
pass1 = rs.getString("password");
session.setAttribute("username",user1);
}
if((user1.equals(user))&&(pass1.equals(pass))){
response.sendRedirect("jsp/welcome.jsp");
}
else{
response.sendRedirect("jsp/login.jsp");
}
}
catch(Exception e){}
}
}

3)
<%
String user = session.getAttribute("username").toString();
%>
Welcome <%out.println(user);%>
<br>
<br>
<a href="#">Add</a><br>
<a href="#">Edit</a><br>
<a href="#">Delete</a>

Thanks









Related Pages:
web interface
web interface  hii sir, i want to develop a project name web GU interface for oracle like 10g can anyone help me plz how to develop using jsp and servelets provide code for WOI
reain values
reain values  Sir I have Jsp page for regisering customers,when i submit form it goes to servlet in servelets it checks for the username if it already exits i m using reuest.sendredirect to same jsp but all textbox values

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.