Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Servlet-Interview-Questions java servlet help neeed....
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


animesh
java servlet help neeed....
2 Answer(s)      3 years and 4 months ago
Posted in : Servlet Interview Questions


i have little bit miss concept on servlet and jsp....what ever i know always the jsp provoke first then servlet ...like the index .jsp will be called first then from there according to action the servlet will be called (mapping i have to make in web.xml)......but to some one told me always the servlet will be called first then jsp...same for index .jsp also
his concept is like

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

the welcome file is like a controller(servlet) which will be provoked first then the index.jsp will be called ...from there any other servlet....etc


i wnt to know which concept is true...mine or his...?

and is welcome file is a controller(servlet)....which calls first

Advertisement
View Answers

August 11, 2011 at 1:02 PM


You can run an application either by jsp or by servlet. It may depend which type of application you are implementing. Suppose you have a login form which obviously a jsp page. And you want to perform an action using servlet to verify the user.

1)login.jsp:

<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
 alert("Enter Username!");
  return false;
}
if(password==""){
 alert("Enter Password!");
  return false;
}
return true;
}
</script>
<form name="form" method="post" action="../Login" onsubmit="javascript:return validate();">
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Login"></td></tr>
</table>
</form>
</html>

2)Login.java:

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

public class Login extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String user=request.getParameter("user");
String pass=request.getParameter("pass");
 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+"'");
int count=0;
          while(rs.next())
          {

                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
          }
          else
          {
                       response.sendRedirect("/examples/jsp/login.jsp");
          }
    }catch (Exception e) {
    e.printStackTrace();
    }
}
}

3)In web.xml, do the servlet mapping:

<servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>Login</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login</url-pattern>
    </servlet-mapping>

So the above application will run through jsp which then called the servlet to perform action. You already know that jsp is converted into servlet during execution.

For more information, visit the following links:

JSP Tutorials:

Servlet Tutorials:



August 11, 2011 at 2:07 PM


ya your answer is correct....here the jsp is fist invoked...and from there the servlet invoked according to the action defined in the form...and request dispatcher 
but can u give a example where the servlet invoke first...before jsp..


actually i am having a fight with my senior...he is telling always the servlet will be invoked first...from there jsp..
and my opinion is first jsp then from there servlet.(jsp converts to servlet when compile that is not the issue)
he is telling the welcome-file is a servlet ...

and my one more question is ,is welcome-file servlet...?

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


Related Tutorials/Questions & Answers:
java servlet help neeed....
java servlet help neeed....  i have little bit miss concept on servlet and jsp....what ever i know always the jsp provoke first then servlet ...like... the servlet will be called (mapping i have to make in web.xml)......but to some one told
help on project - JSP-Servlet
help on project  Need help on Java Project
Advertisements
Help in completing Servlet code!
Help in completing Servlet code!  Complete the following Servlet codes to store the information read from the input form to session object. public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
help me - JSP-Servlet
help me   how to open one compiled html file by clicking one button from jsp
jsp help - JSP-Servlet
jsp help  hai friends i am using videos,which will play on my webpage using flowplayer.my doubt is where will i store my videos whether in the database or in another local folder. please help me as soon as possible thanks
Please Help - JSP-Servlet
Please Help  Respected Sir/Madam, I am R.Ragavendran.. Thanks for your response.. You told me that its difficult to handle the problem.. Ok,Let me try my hands on it.. The help i need is can u please tell me
plz help me!!!!!!!! - JSP-Servlet
plz help me!!!!!!!!  i`ve set the environment varaibles for tomcat as well as jdk.. but i`m not understanding where i`ve to save my servlet..i.e. html file,.java file and xml file.. under which directory or folder i`ve to save
please help - JSP-Servlet
To Year "> please help
Help With Java...
Help With Java...  Write a java program that can take a positive integer greater than 2 as input and write out the number of times one must repeatedly divide this number by 2 before getting a value less than 2
java help!
java help!   I need help starting off a program I am trying to write for class. The class should contain a private instance variable to represent temperature in Degrees Celsius and it should have the following methods
help in java
help in java   Rational Numbers Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber
help in java
help in java  Write an application that prompt the user to enter a number to use as an array size, and then attempt to declare an array using the entered size. Java generates a NegativeArraySizeException if wou attempt to create
java help?
java help?  Write a program, where you first ask values to an array with 6 integer values and then count the sum and average of the values in methods (send in the array & return the counted value to the main program). Print
Project realated help - JSP-Servlet
Project realated help   Hi sir i need your assistance em stuck at a point,actualy i am making final year project,i am making OTRS system in java em using sturts ,servlet,i push whole data by layered architecture,sir succesfully
java help
java help   Write a Java program that does the following: 1.Creates a grading program based on the Baker College grading standard. You will need to look up the current grade standard. You may use only the letter grades without
java help
java help  How to Open CSV Files in a Microsoft Excel Application Using Java Code with example pgm
help for java
help for java  1.convert the following Pseudo code to java if/else structure if student?s grade is greater than or equal to 90 Print ?A? else if student?s grade is greater than or equal to 80 Print ?B? else
help in java
help in java  3.Flights Design and implement a class called Flight that represents an airline flight. It should contain instance data that represents the airline name, flight number, and the flight's origin and destination cities
help in java
help in java  Write a program that will evaluate simple expressions such as 17 + 3 and 3.14159 * 4.7. The expressions are to be typed in by the user. The input always consist of a number, followed by an operator, followed
help in java
help in java  The factorial of a nonnegative integer n is written n! (pronounced √Ę‚?¨Ň? n factorial√Ę‚?¨¬?) and is defined as follows: n!=n . (n-1) . (n-2) . .... . 1 (for values of n greater than or equal to 1) and n!=1 (for n
help in java
help in java  Design and implement a class called Sphere that contains instance data that represents the sphere's diameter. Define the Sphere constructor to accept and initialize the diameter, and include getter and setter
help in java
help in java  Gross And Dozens Design and implement a class called GrossAndDozens to convert a given number of eggs into the number of gross, the number of dozens, and to the number of left over eggs. If you have N eggs, then you
help in java
help in java  Shopping cart Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use
help in java
help in java   Team Roster Create a program called TeamRoster that uses a class called Roster. Roster should contain a team name and the names of all... or help me on this pleas.. 7. Gross And Dozens Design and implement a class
java help
java help  1) build class(es) for the dresses, choose (dress ID, color, price, designer)( link lists) 2) construct the driver class by establishing two separate objects for each sister 3) let them both to enter the dresses
java help
java help  Rational numbers are numbers that can be represented as a fraction p / q where p is an integer number and q is a positive integer (q != 0). Design and implement a Java class RationalNumber for representing such numbers
help in java
help in java  Create 3 shopping carts from a Cart class. Each class should have four items in it, a title, a price for the 4 items and the number of each. Your shopping cart should use private variables, so use getter and setter
help in java
help in java  Create a class named Order that performs order processing of a single item. The class has five instance variables (fields) : customer name, customer number, quantity ordered, unit price, and total price. Define
help in java
help in java  Define a class named Money whose objects represent amounts of U.S. money. The class will have two instance variables of type int for the dollars and cents in the amount of money. Include a constructor with two
help in java
help in java  Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have a number
help in java
help in java  Temperature Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have
 

 

 

DMCA.com