码迷,mamicode.com
首页 > 编程语言 > 详细

mac下用Netbean下编写一个javaservlet

时间:2014-10-28 09:16:40      阅读:330      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   java   for   

1.新建一个java web project

bubuko.com,布布扣

bubuko.com,布布扣注:netbean 安装时会询问是否安装tomcat,如果此处没有tomcat,就是安装时未选tomcat。

完成后

bubuko.com,布布扣

2.编写index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Murach‘s Java Servlets and JSP</title>
    <link rel="stylesheet" href="main.css" type="text/css"/>//main.css是配置文件,注意路径
</head>
<body>
    <h1>Join our email list</h1>
    <p>To join our email list, enter your name and
       email address below.</p>
 <form action="EmailListServlet" method="post">
        <input type="hidden" name="action" value="add">

        <label>Email:</label>
        <input type="email" name="email" required><br>

        <label>First Name:</label>
        <input type="text" name="firstName" required><br>

        <label>Last Name:</label>
        <input type="text" name="lastName" required><br>

        <label>&nbsp;</label>
        <input type="submit" value="Join Now" id="submit">
    </form>
</body>
</html>

3.编写main.css

body { 
    font-family: Arial, Helvetica, sans-serif;
    font-size: 11pt;
    margin-left: 2em;
    margin-right: 2em;
}
h1 { 
    color: teal;
}
label {
    float: left;
    width: 6em;
    margin-bottom: 0.5em;
}
input[type="text"], input[type="email"] {
    width: 15em;
    margin-left: 0.5em;    
    margin-bottom: 0.5em;
}
br {
    clear: both;
}
#submit {
    margin-left: 0.5em; }

3.两个java文件

EmailListServlet.java 和User.java

package murach.email;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import murach.business.User;
//import murach.data.UserDB;

public class EmailListServlet extends HttpServlet  {

    @Override
    protected void doPost(HttpServletRequest request, 
                          HttpServletResponse response) 
                          throws ServletException, IOException {

        String url = "/index.html";

        // get current action
        String action = request.getParameter("action");
        if (action == null) {
            action = "join";  // default action
        }
        // perform action and set URL to appropriate page
        if (action.equals("join")) {
            url = "/index.html";    // the "join" page
        }
        else if (action.equals("add")) {                
            // get parameters from the request
            String firstName = request.getParameter("firstName");
            String lastName = request.getParameter("lastName");
            String email = request.getParameter("email");

            // store data in User object and save User object in db
            User user = new User(firstName, lastName, email);
           // UserDB.insert(user);
            
            // set User object in request object and set URL
            request.setAttribute("user", user);
            url = "/thanks.jsp";   // the "thanks" page
        }
        
        // forward request and response objects to specified URL
        getServletContext()
            .getRequestDispatcher(url)
            .forward(request, response);
    }    
 @Override
    protected void doGet(HttpServletRequest request, 
                         HttpServletResponse response) 
                         throws ServletException, IOException {
        doPost(request, response);
    }    
}


package
murach.business; import java.io.Serializable; public class User implements Serializable { private String firstName; private String lastName; private String email; public User() { firstName = ""; lastName = ""; email = ""; } public User(String firstName, String lastName, String email) { this.firstName = firstName; this.lastName = lastName; this.email = email; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }

4.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <servlet>
        <servlet-name>EmailListServlet</servlet-name>
        <servlet-class>murach.email.EmailListServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>EmailListServlet</servlet-name>//要到EmailListServlet这个java文件中去寻找
        <url-pattern>/EmailListServlet</url-pattern>//需要找到EmailListServlet这个方法
    </servlet-mapping>

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
<welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

5.thanks.jsp

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Murach‘s Java Servlets and JSP</title>
    <link rel="stylesheet" href="main.css" type="text/css"/>
</head>

<body>
    <h1>Thanks for joining our email list</h1>

    <p>Here is the information that you entered:</p>

    <label>Email:</label>
    <span>${user.email}</span><br>
    <label>First Name:</label>
    <span>${user.firstName}</span><br>
    <label>Last Name:</label>
    <span>${user.lastName}</span><br>
<p>To enter another email address, click on the Back 
    button in your browser or the Return button shown 
    below.</p>

    <form action="" method="get">
        <input type="hidden" name="action" value="join">
        <input type="submit" value="Return">
    </form>

</body>
</html>

6.文件分布如图

bubuko.com,布布扣

注:web.xml文件必须在WEB-INF这个文件夹下,不然点击submit后会报错。

7.运行结果

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

 

mac下用Netbean下编写一个javaservlet

标签:style   blog   http   io   color   os   ar   java   for   

原文地址:http://www.cnblogs.com/hybill/p/4055905.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!