/ W3SCHOOLS

W3schools - JSP_Redirect / Email

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Page Redirect

Is generally used when a document moves to a new location and we need to send the client to this new location

This can be because of load balancing, or for simple randomization

The simplest way of redirecting a request to another page is by using sendRedirect() of response object

Public void response.sendRedirect(String location) throws IOException

This method sends back the response to the browser along with the status code and new page location

Can also use the setStatus() and the setHeader() together to achieve the same redirection

String site = “https://spongebob53.github.io/”;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader(“Location”, site);

Sending Email

To send a email using a JSP, should have the JavaMail API and the Java Activation Framework (JAF) installed on your machine

You need to add the mail.jar and the activation.jar files in your classpath

<%@ page import = "java.io.*,java.util.*,javax.mail.*"%>
<%@ page import = "javax.mail.internet.*,javax.activation.*"%>
<%@ page import = "javax.servlet.http.*,javax.servlet.*" %>

<%
    String result;
    
    // Recipient's email ID needs to be mentioned.
    String to = "abcd@gmail.com";
    
    // Sender's email ID needs to be mentioned
    String from = "mcmohd@gmail.com";
    
    // Assuming you are sending email from localhost
    String host = "localhost";
    
    // Get system properties object
    Properties properties = System.getProperties();
    
    // Setup mail server
    properties.setProperty("mail.smtp.host", host);
    
    // Get the default Session object.
    Session mailSession = Session.getDefaultInstance(properties);
    
    try {
        // Create a default MimeMessage object.
        MimeMessage message = new MimeMessage(mailSession);
        
        // Set From: header field of the header.
        message.setFrom(new InternetAddress(from));
        
        // Set To: header field of the header.
        // If want to send an email to multiple recipients, to specify multiple email IDs
        message.addRecipient(Message.RecipientType.TO,
                   new InternetAddress(to));
        // Set Subject: header field
        message.setSubject("This is the Subject Line!");
        
        // Now set the actual message
        message.setText("This is actual message");
        
        // Send message
        Transport.send(message);
        result = "Sent message successfully....";
    } catch (MessagingException mex) {
        mex.printStackTrace();
        result = "Error: unable to send message....";
    }
%>
    
<html>
    <head>
        <title>Send Email using JSP</title>
    </head>
    
    <body>
        <center>
            <h1>Send Email using JSP</h1>
        </center>
        
        <p align = "center">
            <% 
                out.println("Result: " + result + "\n");
            %>
        </p>
    </body>
</html>