首页 > 代码库 > mac下用Netbean下编写一个javaservlet

mac下用Netbean下编写一个javaservlet

1.新建一个java web project

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

完成后

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="http://www.mamicode.com/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="http://www.mamicode.com/join">        <input type="submit" value="http://www.mamicode.com/Return">    </form></body></html>

6.文件分布如图

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

7.运行结果

 

mac下用Netbean下编写一个javaservlet