在Netbeans中更新Java Web应用程序的Web页面

时间:2014-05-08 13:27:52

标签: java html web-applications netbeans deployment

我做了一个简单的Email Sender,它创建了一个页面enter image description here,您可以在其中发送邮件。该程序有效,但我无法通过更改HTML文件来修改页面。这是我的代码:

    package com.example;

import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name = "EmailServlet", urlPatterns = {"/EmailServlet"})
public class EmailServlet extends HttpServlet {

    @EJB
    private EmailSessionBean emailBean;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String to = request.getParameter("to");
        String subject = request.getParameter("subject");
        String body = request.getParameter("body");

        emailBean.sendEmail(to, subject, body);

        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet EmailServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Form Submitted</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

package com.example;

import javax.mail.*;
import java.util.Date;
import java.util.Properties;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


@LocalBean
@Stateless
public class EmailSessionBean {

private int port = ******;
private String host = "**********";
private String from = "***********";
private boolean auth = true;
private String username = "**************";
private String password = "***************";
private Protocol protocol = Protocol.SMTPS;
private boolean debug = true;

public void sendEmail(String to, String subject, String body) {
    Properties props = new Properties();
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    switch (protocol) {
        case SMTPS:
            props.put("mail.smtp.ssl.enable", true);
            break;
        case TLS:
            props.put("mail.smtp.starttls.enable", true);
            break;
    }

    Authenticator authenticator = null;
    if (auth) {
        props.put("mail.smtp.auth", true);
        authenticator = new Authenticator() {
            private PasswordAuthentication pa = new PasswordAuthentication(username, password);

            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return pa;
            }
        };
    }
    Session session = Session.getInstance(props, authenticator);
    session.setDebug(debug);

    MimeMessage message = new MimeMessage(session);
    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] address = {new InternetAddress(to)};
        message.setRecipients(Message.RecipientType.TO, address);
        message.setSubject(subject);
        message.setSentDate(new Date());
        message.setText(body);
        Transport.send(message);
    } catch (MessagingException ex) {
        ex.printStackTrace();
    }
}

}

当我尝试修改以下html文件时,页面没有改变。

    <!DOCTYPE html>

<html>
    <head>
        <title>Email</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <h1>Email</h1>
        <form method="POST" action="EmailServlet">
            <label for="to">To:</label><input id="to" name="to" type="text"/><br/>
            <label for="subject">Subject:</label><input id="subject" name="subject" type="text"/><br/>
            <textarea name="body" cols="60" rows="15"></textarea><br/>
            <input type="submit" value="Send"/>
        </form>
    </body>
</html>

那么我如何才能在网页中看到HTML文件中的更改?

编辑:我想我应该研究一下deployment。如果有人可以指出我如何在Netbeans中重新部署html文件,我仍然很高兴。 编辑:找到它!我需要保存文件:/不知怎的,我之前没有保存,浪费了很多时间才意识到这一点!

1 个答案:

答案 0 :(得分:0)

请记住保存修改后的html文件,以便在服务器上进行可视化更改。 enter image description here