troubleshooting Question

simple example of servlet

Avatar of siddharthkool
siddharthkool asked on
JavaJava EE
7 Comments1 Solution424 ViewsLast Modified:
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.
<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>
ASKER CERTIFIED SOLUTION
Pramod Kumar

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros