[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

servlet example issue with method

Posted on 2012-08-13
12
Medium Priority
?
607 Views
Last Modified: 2012-08-27
I was trying following example getting error as inb attachment saying method wont suppoerted here.Any ideas, suggestions, sample code, links, source code highly appreciated. Thanks in advance

test.html



<%@page contentType="text/html" pageEncoding="UTF-8"%>  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
   "http://www.w3.org/TR/html4/loose.dtd">  
 
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
        <title>JSP Page</title>  
    </head>  
    <body>  
   
         <form action="AdminCheck" method="post">  
              Login ID <input type="text" value="user"/><br>  
              Password <input type="password" value="pass"/><br><br><br>  
              <input type="submit" value="OK"/><br>  
         </form>  
    </body>  
</html>  




AdminCheck.java


import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

public class AdminCheck extends HttpServlet {  

      protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
      throws ServletException, IOException {  
            response.setContentType("text/html;charset=UTF-8");  
            PrintWriter out = response.getWriter();  

            String a=request.getParameter("user");//Username for my JSP page  
            String b=request.getParameter("pass");//Password for a JSP page  

            try {  


                  out.println("<html>");  
                  out.println("<head>");  
                  out.println("<title>Servlet admincheck</title>");    
                  out.println("</head>");  
                  out.println("<body>");  
                  if(a.equals("admin")&& b.equals("check"))  
                        out.println("Enter successfully");  
                  else  
                        out.println("wrong");  
                  out.println("<h1>Servlet admincheck at " + request.getContextPath () + "</h1>");  
                  out.println("</body>");  
                  out.println("</html>");  

            } finally {    
                  out.close();  
            }  
}
}




web.xml




<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
  <servlet>
    <servlet-name>Admin</servlet-name>
    <servlet-class>AdminCheck</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Admin</servlet-name>
    <url-pattern>/AdminCheck</url-pattern>
  </servlet-mapping>
 
</web-app>
ServCR.JPG
0
Comment
Question by:gudii9
  • 8
  • 4
12 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 38289918
Your servlet should override doPost to support your form's action
0
 
LVL 7

Author Comment

by:gudii9
ID: 38291838
I changed the servlet as below
import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

public class AdminCheck extends HttpServlet {  
      protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {

            
      }
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
      throws ServletException, IOException {  
            response.setContentType("text/html;charset=UTF-8");  
            PrintWriter out = response.getWriter();  

            String a=request.getParameter("user");//Username for my JSP page  
            String b=request.getParameter("pass");//Password for a JSP page  

            try {  


                  out.println("<html>");  
                  out.println("<head>");  
                  out.println("<title>Servlet admincheck</title>");    
                  out.println("</head>");  
                  out.println("<body>");  
                  if(a.equals("admin")&& b.equals("check"))  
                        out.println("Enter successfully");  
                  else  
                        out.println("wrong");  
                  out.println("<h1>Servlet admincheck at " + request.getContextPath () + "</h1>");  
                  out.println("</body>");  
                  out.println("</html>");  

            } finally {    
                  out.close();  
            }  
}
}

Now HTML leading to servlet but showing empty servlet .

How can I call processRequest method from doPost() method. Please advise
EmptySErvlet.JPG
0
 
LVL 7

Author Comment

by:gudii9
ID: 38291845
I changed as


import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

public class AdminCheck extends HttpServlet {  
      protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
            processRequest(req, res);
            /*PrintWriter pw = res.getWriter();
            pw.println("<hr/>");
            pw.println("Thank You for Shopping at BuyForLess.com. Please Visiting again. Hava a nice day!");*/
      }
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)  
      throws ServletException, IOException {  
            response.setContentType("text/html;charset=UTF-8");  
            PrintWriter out = response.getWriter();  

            String a=request.getParameter("user");//Username for my JSP page  
            String b=request.getParameter("pass");//Password for a JSP page  

            try {  


                  out.println("<html>");  
                  out.println("<head>");  
                  out.println("<title>Servlet admincheck</title>");    
                  out.println("</head>");  
                  out.println("<body>");  
                  if(a.equals("admin")&& b.equals("check"))  
                        out.println("Enter successfully");  
                  else  
                        out.println("wrong");  
                  out.println("<h1>Servlet admincheck at " + request.getContextPath () + "</h1>");  
                  out.println("</body>");  
                  out.println("</html>");  

            } finally {    
                  out.close();  
            }  
}
}


but still coming empty servlet. PLEase advise
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 38292220
import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  

public class AdminCheck extends HttpServlet {  
      /*protected void doPost(HttpServletRequest req, HttpServletResponse res)
      throws ServletException, IOException {
            processRequest(req, res);
            PrintWriter pw = res.getWriter();
            pw.println("<hr/>");
            pw.println("Thank You for Shopping at BuyForLess.com. Please Visiting again. Hava a nice day!");
      }*/
      protected void doPost(HttpServletRequest request, HttpServletResponse response)  
      throws ServletException, IOException {  
            response.setContentType("text/html;charset=UTF-8");  
            PrintWriter out = response.getWriter();  

            String a=request.getParameter("user");//Username for my JSP page  
            String b=request.getParameter("pass");//Password for a JSP page  

            try {  


                  out.println("<html>");  
                  out.println("<head>");  
                  out.println("<title>Servlet admincheck</title>");    
                  out.println("</head>");  
                  out.println("<body>");  
                  if(a.equals("admin")&& b.equals("check"))  
                        out.println("Enter successfully");  
                  else  
                        out.println("wrong");  
                  out.println("<h1>Servlet admincheck at " + request.getContextPath () + "</h1>");  
                  out.println("</body>");  
                  out.println("</html>");  

            } finally {    
                  out.close();  
            }  
}
}

Above also did not work throowing blank servlet page. Please advise
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38292601
What did the log file say?
0
 
LVL 7

Author Comment

by:gudii9
ID: 38292807
I do not know the location of the log. What would be default tomcat log folder location. I searched on *.log files and found many entries. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 38300770
I checked following folder

C:\softwares gp\apache-tomcat-7.0.29\apache-tomcat-7.0.29\logs


which is empty. Please advise
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38300836
If it's absolutely empty that's possibly not the Tomcat you're using
0
 
LVL 7

Author Comment

by:gudii9
ID: 38305840
How do I find the log folder relating to my tomat. Is there is a way i can see properties of server and find. Please advise
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38307605
You need to look at the docs for your version of Tomcat
0
 
LVL 7

Author Comment

by:gudii9
ID: 38316899
>>docs for your version of Tomcat

Where can I find them. How to access them. Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 38330017
>>docs for your version of Tomcat

Where can I find them. How to access them. Please advise
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month18 days, 5 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question