This week's book giveaways are in the Cloud and AI/ML forums. We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line! See this thread and this one for details.
Hi, Im new to servlets and JSp.I want to create a login page askin for username and password in JSP.Then this should validate whether the user is in the database ..if so load welcome page else error page. Im using Tomcat 4.1,jdk1.4,Mysql database. I need this urgently so that i can get an idea how it is done and move on. I need the entire source code and running instructions.I do not know how it should be done. Please somebody help!! [ April 10, 2004: Message edited by: Bear Bibeault ] [ April 10, 2004: Message edited by: Bear Bibeault ]
In most cases, it's the other way round : a servlet calls a JSP. The MVC (Model View Controller) model 2 states that the Controller should be a servlet where the view (or presentation) should be dealt with by a JSP. How does a servlet call a JSP ? Well, first of all, you should know that Tomcat does the first calling. If you have a Java web application, this application should have a web.xml file. In this web.xml file you should put the name of the servlet and the URL to which it responds. ANd then the application gets on its way : Tomcat calls the servlet and the servlet calls its first JSP. And this could well be a login.jsp. The communication between the servlet and JSP is done by Java Beans. When you ask a user to log in, Tomcat calls the servlet, the servlet calls the login.jsp. When the user submits his data (username and pasword) via a submit button, the data he/she has filled in in the FORM is transmitted to the servlet. And with this data, the servlet can decide whether the user can be granted access.
Tejaswini, You need , one jsp,one servlet. In the jsp page, put two text boxes and a submit button and in the form action..leads to the servlet. In the servlet service method..check the username and pwd validity by querying the database..if exists..use sendredirect funtion to redirect the browser to the welcome page other wise to the error page...if the user id , password are correct then , store them in the session..so that..in every successive page, we can check the user validity..so user can not be allowed to access the other pages by directly putting the url in the browser... Practise it ..then only u will realise the thing.
posted 15 years ago
You can call a Servlet from JSP by following the steps below 1 - Make a Servlet, lets suppose your servlet name is MySimpleServlet 2- Add the Servlet information in web.xml file by using following tags <servlet> <servlet-name>mysimpleservlet</servlet-name> <servlet-class>your_package_name.MySimpleServlet</servlet-class> </servlet>
3 - Now call the servlet from your jsp as following <form action="/mysimpleservlet" method="GET or POST"> Thats how you can call a Servlet from JSP. If there occur any problem, feel free to post reply.
posted 15 years ago
Thanks a lot every one..i got the hang of it. I have another doubt...wot should be done if i have to display data from the database in my JSP on page load. Thank You.
Originally posted by tejaswini vasist: Thanks a lot every one..i got the hang of it. I have another doubt...wot should be done if i have to display data from the database in my JSP on page load. Thank You.
Call a servlet that loads the DB info and then foward to the JSP.