Event Classes in Servlet

Click to rate this post!
[Total: 1 Average: 5]

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
  1. ServletRequestEvent
  2. ServletContextEvent
  3. ServletRequestAttributeEvent
  4. ServletContextAttributeEvent
  5. HttpSessionEvent
  6. HttpSessionBindingEvent
Event interfaces
  1. ServletRequestListener
  2. ServletRequestAttributeListener
  3. ServletContextListener
  4. ServletContextAttributeListener
  5. HttpSessionListener
  6. HttpSessionAttributeListener
  7. HttpSessionBindingListener
  8. 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 solicitation

logs the IP locations of the PCs from which the solicitations begin ,change

information pressure

encryption and unscrambling

input approval and so on.

Advanced of Filter

Channel 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

  1. 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.

  1. void print(boolean b){}
  2. void print(char c){}
  3. void print(int i){}
  4. void print(long l){}
  5. void print(float f){}
  6. void print(double d){}
  7. void print(String s){}
  8. void println{}
  9. void println(boolean b){}
  10. void println(char c){}
  11. void println(int i){}
  12. void println(long l){}
  13. void println(float f){}
  14. void println(double d){}
  15. void println(String s){}

JAVA Tutorial Video

Example of Registration form

we have created the three pages.

  • register.html
  • Register.java
  • web.xml

register.html

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.

<html>

<body>

<form action=”servlet/Register” method=”post”>

Name:<input type=”text” name=”userName”/><br/><br/>

Password:<input type=”password” name=”userPass”/><br/><br/>

Email Id:<input type=”text” name=”userEmail”/><br/><br/>

Country:

select name=”userCountry”>

<option>India</option>

<option>Pakistan</option>

<option>other</option>

</select>

<br/><br/>

input type=”submit” value=”register”/>

</form>

</body>

</html>

Register.java

This 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 file

The is the configuration file, providing information about the servlet.

<web-app>

<servlet>

<servlet-name>Register</servlet-name>

<servlet-class>Register</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Register</servlet-name>

<url-pattern>/servlet/Register</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>register.html</welcome-file>

</welcome-file-list>

</web-app>

Example of uploading file:

index.html

<html>

<body>

<form action=”go” method=”post” enctype=”multipart/form-data”>

Select File:<input type=”file” name=”fname”/><br/>

<input type=”submit” value=”upload”/>

</form>

</body>

</html>

UploadServlet.java

import 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 file

This configuration file provides information about the servlet.

<web-app>

<servlet>

<servlet-name>UploadServlet</servlet-name>

<servlet-class>UploadServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>UploadServlet</servlet-name>

<url-pattern>/go</url-pattern>

</servlet-mapping>

</web-app>

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

Example:

 index.html

<form action=”servlet/SendMail”>

To:<input type=”text” name=”to”/><br/>

Subject:<input type=”text” name=”subject”><br/>

Text:<textarea rows=”10″ cols=”70″ name=”msg”></textarea><br/>

<input type=”submit” value=”send”/>

</form>

SendMail.java

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class SendMail extends HttpServlet {

public void doGet(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();

String to=request.getParameter(“to”);

String subject=request.getParameter(“subject”);

String msg=request.getParameter(“msg”);

Mailer.send(to, subject, msg);

out.print(“message has been sent successfully”);

out.close();

}

}

Mailer.java

import java.util.Properties;

import javax.mail.*;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

public class Mailer {

public static void send(String to,String subject,String msg){

final String user=”sonoojaiswal@javatpoint.com”;//change accordingly

final String pass=”xxxxx”;

//1st step) Get the session object

Properties props = new Properties();

props.put(“mail.smtp.host”, “mail.javatpoint.com”);//change accordingly

props.put(“mail.smtp.auth”, “true”);

Session session = Session.getDefaultInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(user,pass);

}

});

//2nd step)compose message

try {

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(user));

message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

message.setSubject(subject);

message.setText(msg);

//3rd step)send message

Transport.send(message);

System.out.println(“Done”);

catch (MessagingException e) {

throw new RuntimeException(e);

}

}

}

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


100% Secure Payments. All major credit & debit cards accepted.

Call Now Button