siddharthkool
asked on
simple example of servlet
i have an HTML page in that i am entering username.
this username i want to store in text file in servlet .
example : If it is a new user, i want to reply to the user with a message: userName is registered successfully!eg. John is registered successfully! . If this username has been used, i want to reply to the user with a message:userName has been registered already!(eg. John has been registered already!).
but if i enter andy once again it should give me an error saying username already exixts.
can anyone help.
this username i want to store in text file in servlet .
example : If it is a new user, i want to reply to the user with a message: userName is registered successfully!eg. John is registered successfully! . If this username has been used, i want to reply to the user with a message:userName has been registered already!(eg. John has been registered already!).
but if i enter andy once again it should give me an error saying username already exixts.
can anyone help.
<html>
<head>
<title>doGetMethod</title>
</head>
<body>
<h3>Please enter Username :</h3><p>
<form action="/forwardMethod/test">
Username : <input type="text" name="username"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------------------
forwarded.java
package forwardMethod;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ForwardedServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
try
{
String temp = (String) req.getAttribute("temperature");
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>" + "Input " + temp + " is not valid! </h3><p>");
pw.println("</body>");
pw.println("</html>");
}
catch(Exception e)
{
}
}
}
-------------------------------------------------------------------------------------------
testservlet.java
package forwardMethod;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Vector;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
doPost(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException
{
String username = req.getParameter("username");
try
{
PrintWriter out = res.getWriter();
if(username == "")
{
out.println("<h3>username not specified</h3>");
return;
}
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("<h3>" + username + " Has Been Registered</h3><p>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
req.setAttribute("username", username);
req.getRequestDispatcher("/forward").forward(req, res);
return;
}))
==================================================================
xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>TestServlet</servlet-name>
<servlet-class>forwardMethod.TestServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ForwardedServlet</servlet-name>
<servlet-class>forwardMethod.ForwardedServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ForwardedServlet</servlet-name>
<url-pattern>/forward</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Erro only u need right ??
Before insert the user name u must check the user exiting or not ...
if exiting menas throw an error to a one jsp page ..
else insert !!
if exiting menas throw an error to a one jsp page ..
else insert !!
Using a text file is not really scalable. You ought to use a database
Check this out, there is a Logging Servlet Access example using Filters:
http://java.sun.com/products/servlet/Filters.html
http://java.sun.com/products/servlet/Filters.html
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
awesome!!!
thanks!!
thanks!!
ASKER
dear pramodkrjsr,
above code works fine however when i enter user name andy , it gets stored in data.txt.
it also gives error if i retype andy . how ever if i typed sam , it gets written in data.text , but if i try to login by same name sam it should give me an message user already register. but it does not and duplicate entry of sam gets store.
code works fine only for first user , after that duplicate entries of other name gets store.
do i have to add an for loop for reading data in text file ? because it seems its reading and checking only fist name in the data.txt. ?
above code works fine however when i enter user name andy , it gets stored in data.txt.
it also gives error if i retype andy . how ever if i typed sam , it gets written in data.text , but if i try to login by same name sam it should give me an message user already register. but it does not and duplicate entry of sam gets store.
code works fine only for first user , after that duplicate entries of other name gets store.
do i have to add an for loop for reading data in text file ? because it seems its reading and checking only fist name in the data.txt. ?