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
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:
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>
Ads