Servlet and JSP
Servlet and JSP
ex;http://localhost:7810/Al13/add?+num1=2&+num2=5
4)HttpServletResponse and HttpServletRequest= both are objexts in which data is embedded to talk btw
server and user
5) httpsession= //if we put our k value in a session we can use it for long in multiple
servlets //provided u dont delete it
6) cOOKI IS ALT TO SESSION
above example hav cookie and session htoh done
We can put speciic data for a sercvlet by adding param under name of servlet in xtl file
Conig with diffeerent data
*) SERVLET ANNOTATION AND CONFIGURATION = shorthand to remove need of xml file completelty bt
remember to keep your xml file completely clean for this
JSP = writing j java in html code n its a way to avoid servlet methods n all bt still more to come
<% %> scriptlets ,anytiing inside it will go to service method
<%! %. Declaration tag and anyhing init will go outside servcice method ;like we want to make a integer
or anything else lik a method will all go into this tag
<%@ %> import a package is done with this ex; <%@ page import =”java.util.date,java.sql.stm,...” &>
deractive tag
Want tot print something <%= k %> will rpint k ,expresssion tag
@page examples
@include example
This ex is to show we made a 2nd jsp file called j2Al18 and included it as a file in jAl18 to get its code
@taglib example
<fx:Chris> its lik we are making our own tag with customized skin using library
session scope last for whole session and pageContext scope last for a page BUT
1 import java.sql.*
2 load and register driver
3
2ND SOURCE
Static webpages – pages stored on servers and which provide same response to
any user are called static webpages-used html,css,js
Dynamic webpages – pages not stored on server but we write program and put it
on server which controls the information coming in at run time – this programs
gives a way for coming in data to db and embeddes the sitting static pages with
useful data
Servlet are server side programs written in java/its an interface/its an api for
server side programming/its used to generate dyn web page
RequestDispatcher(rd)
req.setAttribute("refermewiththis", d);
then in second servlet we extract value by
now requestdipatcher forwards the same req to another servlet that holds our
data but in sendredirect thats not the case so we use session management
concept to transfer data
SendRedirect
res.sendRedirect("sq");
in first servlet – we use the sendredirect method which is of res obj so we
combine it with it and use that name ‘sq’ used in mapping
Session method
In first servlet-We make a HttpSession obj and as its an interface we use req obj
to initialize it
Then
session.setAttribute(“refermebythisinnextsrvlet”,k);
Cookie method
First servlet - In cookie method we make Cookie cookie obj and call its
constructor add in name of value and value
Res.addCookie(cookie);
Second servlet – her we make an array of cookies as the client will send all the
cookies its having with it and we need to accept the one we want so we will use a
for special loop
For(Cookie c:cookie){
}
ServletConfig and ServletContext
We need this both to get initial values for app or the servlet
@WebServlet(“name”) thats it
NOW JSP
Expression tag <%= whatever in it will go in into out.print i.e wl b printed %>
For DIRECTIVE
Language=”scripting language”
Extends=”className”
Import=”ImportLiat”
Session=”true/false”
autoFlush=”true/false”
contentType=”ctinfo”
errorPage=”error_url”
info=”information”
isThreadSafe=”true/false”
‘include’
Request
Response
PageContext
Out
Session
Application
Config
In page where we are getting error go in <%@ %> tag and add
errorPage=”nameofthepagewhereyouhavealtthingstoshow” thats it
JDBC IN JSP
<% Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/profile";
Connection con=
DriverManager.getConnection(url,"root","chris");
Statement stm = con.createStatement();
String sql = "select * from data where ID=1";
ResultSet rs = stm.executeQuery(sql);
rs.next();
%>
ID : <%= rs.getString(1) %> <br>
Name : <%= rs.getString(2) %> <br>
Cell : <%= rs.getString(3) %> <br>
MVC USING SERVLET AND JSP
Like ${} can be used to fetch the data sent btwn servlet and jsp
${s}
</c:forEach>
SERVLET FILTERS
We mak HttpServletRequest obj here and check our conditions as such by if/else
LOGIN
We can access videos on a website easily by using url for that page . So to stop it
we use logic of session where credentials of a user are carried throughout whole
time until he doesnt logout
LOGIN.JAVA
if(name.equals("Chris") &&
pass.equals("Rock")) {
HttpSession session =
request.getSession();
session.setAttribute("A", name);
response.sendRedirect("welcome.jsp");
}
else {
response.sendRedirect("about.jsp");
}
WELCOME.JSP AND VIDEOS.JSP
<%
if(session.getAttribute("A")==null)
{
response.sendRedirect("login.jsp");
}
%>
LOGOUT.JAVA
HttpSession session = request.getSession();
session.removeAttribute("A");
session.invalidate();
response.sendRedirect("login.jsp");
We made a diff class with database code and later we made a nobject of it in our
login.java of it and checked all of that
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection(url,"root","chris");
PreparedStatement stm =
con.prepareStatement(sql);
stm.setString(1, name);
stm.setString(2, pass);
ResultSet rs =stm.executeQuery();
if(rs.next()) {
return true;
}