
Event Classes in Servlet :
Occasions are fundamentally event of something. Changing the condition of a protest is known as an occasion. We can play out some vital errands at the event of these special cases, for example, including aggregate and current logged clients, making tables of the database at time of conveying the venture, making database association protest and so on.
Event classes
- ServletRequestEvent
- ServletContextEvent
- ServletRequestAttributeEvent
- ServletContextAttributeEvent
- HttpSessionEvent
- HttpSessionBindingEvent
Event interfaces
- ServletRequestListener
- ServletRequestAttributeListener
- ServletContextListener
- ServletContextAttributeListener
- HttpSessionListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- HttpSessionActivationListener
Servlet Filter:
A channel is a protest that is summoned at the preprocessing and postprocessing of a demand. It is essentially used to perform sifting errands, for example, transformation, logging, pressure, encryption and decoding, input approval and so on.
The servlet channel is pluggable, i.e. its entrance is characterized in the web.xml record, on the off chance that we expel the passage of channel from the web.xml document, channel will be expelled naturally and we don't have to change the servlet. So support cost will be less.
Use of Filter:recording every approaching solicitationlogs the IP locations of the PCs from which the solicitations begin ,changeinformation pressureencryption and unscramblinginput approval and so on.Advanced of FilterChannel is pluggable.One channel don't have reliance onto another asset.Less Maintenance.ServletInputStream class:ServletInputStream class gives stream to peruse parallel information, for example, picture and so forth from the demand protest. It is a dynamic class.
The getInputStream() strategy for ServletRequest interface restores the case of ServletInputStream class. So can be get
Method of ServletInputStream class
- int readLine(byte[] b, int off, int len)
ServletOutputStream class:
ServletOutputStream class gives a stream to compose twofold information into the reaction. It is a conceptual class.
The getOutputStream() technique for ServletResponse interface restores the occasion of ServletOutputStream class. It might be get
Methods of ServletOutputStream class
The ServletOutputStream class provides print() and println() methods that are overloaded.- void print(boolean b){}
- void print(char c){}
- void print(int i){}
- void print(long l){}
- void print(float f){}
- void print(double d){}
- void print(String s){}
- void println{}
- void println(boolean b){}
- void println(char c){}
- void println(int i){}
- void println(long l){}
- void println(float f){}
- void println(double d){}
- void println(String s){}
JAVA Tutorial Video
[embed]https://www.youtube.com/watch?v=UbpbqqIw_3Q&t=1725s[/embed]Example of Registration form we have created the three pages.- register.html
- Register.java
- web.xml
In this page, we have getting contribution from the client utilizing content fields and combobox. The data entered by the client is sent to Register servlet, which is mindful to store the information into the database.
Register.javaThis servlet class gets every one of the information entered by client and stores it into the database. Here, we are playing out the database rationale. Be that as it may, you may isolate it, which will be better for the web application. import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;import javax.servlet.http.*;public class Register extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();String n=request.getParameter("userName");String p=request.getParameter("userPass");String e=request.getParameter("userEmail");String c=request.getParameter("userCountry");try{Class.forName("oracle.jdbc.driver.OracleDriver");Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");PreparedStatement ps=con.prepareStatement("insert into registeruser values(?,?,?,?)");ps.setString(1,n);ps.setString(2,p);ps.setString(3,e);ps.setString(4,c);int i=ps.executeUpdate();if(i>0)out.print("You are successfully registered...");}catch (Exception e2) {System.out.println(e2);}out.close();}}web.xml fileThe is the configuration file, providing information about the servlet.Example of uploading file:
index.htmlUploadServlet.javaimport java.io.*;import javax.servlet.ServletException;import javax.servlet.http.*;import com.oreilly.servlet.MultipartRequest;public class UploadServlet extends HttpServlet {public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html");PrintWriter out = response.getWriter();MultipartRequest m=new MultipartRequest(request,"d:/new");out.print("successfully uploaded");}}web.xml fileThis configuration file provides information about the servlet.Sending email
The JavaMail API gives many classes that can be utilized to send email from java. The javax.mail and javax.mail.internet bundles contains every one of the classes required for sending and accepting messages.For better comprehension of this illustration click ventures for sending email from javamail programming interface.- mail.jar
- activation.jar