yiqianyan
yiqianyan
采纳率90.9%
2017-03-28 12:46

jsp+servlet+jdbc+web.xml

2
  • xml
  • java
已采纳

message.jsp:

 <form name="formName" method="post"
                                    action="servlet/MessageAddServlet">
                                    <tr>
                                        <td width="12%">昵称:</td>
                                        <td width="88%"><input name='nickname' type='text' /> *</td>
                                    </tr>
                                    <tr>
                                        <td>头像:</td>
                                        <!-- 多个单选按钮配合使用时,name值相等,value值不相等,
                                        因为处理页面根据value值才能知道选择的是哪一个单选按钮对应的内容 -->
                                        <td><input name="sex" type="radio" value="1"
                                            checked="checked"> <img src="img/1.gif" width="64"
                                            height="71"> <input type="radio" name="sex" value="2">
                                            <img src="img/2.gif" width="64" height="71"> <input
                                            type="radio" name="sex" value="3"> <img
                                            src="img/3.gif" width="64" height="71"> <input
                                            type="radio" name="sex" value="4"> <img
                                            src="img/4.gif" width="64" height="71"> <input
                                            type="radio" name="sex" value="5"> <img
                                            src="img/5.gif" width="64" height="71"></td>
                                    </tr>
                                    <tr>
                                        <td>QQ:</td>
                                        <td><input name='qq' type='text'  /></td>
                                    </tr>
                                    <tr>
                                        <td>邮箱:</td>
                                        <td><input name='email' type='text' /></td>
                                    </tr>
                                    <tr>
                                        <td>电话:</td>
                                        <td><input name='tel' type='text' /></td>
                                    </tr>
                                    <tr>
                                        <td>内容:</td>
                                        <td><textarea name="content" cols="50" rows="10"
            ></textarea> *</td>
                                    </tr>
                                    <tr>
                                        <td>&nbsp;</td>
                                        <td><input type="submit" name="submit" value="提交"
                                            onClick="return checklyb();"
                                            style="height: 19px; border: solid 1px #000000; color: #666666" />
                                            <input type="reset" name="reset" value="重置"
                                            style="height: 19px; border: solid 1px #000000; color: #666666" /></td>
                                    </tr>
                                </form>

web.xml:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>hotel2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>MessageAddServlet</servlet-name>
    <servlet-class>servlet.MessageAddServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MessageAddServlet</servlet-name>
    <url-pattern>/servlet/MessageAddServlet</url-pattern>
  </servlet-mapping>
</web-app>

MessageAddServlet.java:

 package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javabean.Message;
public class MessageAddServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();

        String nickname = request.getParameter("nickname");
        String sex= request.getParameter("sex");
        String qq = request.getParameter("qq");
        String email = request.getParameter("email");
        String tel = request.getParameter("tel");
        String content = request.getParameter("content");

        Message m=new Message();
        m.excuteServlet(nickname,sex,qq,email,tel,content);
        //js出现在servlet中
        out.print("<script>alert('添加成功!!');location.href='lyblist.jsp';</script>");
    }

}

Message.java:

 package javabean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import db.DBUtil;

public class Message {
    private int message_id;
    private String  nickname;
    private String sex;
    private String qq;
    private String email;
    private String tel;
    private String content;
    private String message_addedtime;
    private String content_reply;

    private Connection conn;
    private  PreparedStatement ptmt;
    private ResultSet rs;

    private String sql;

    public void excuteServlet(String nickname,String sex,String qq,String email,String tel,String content){
        sql = "insert into message(nickname,sex,qq,email,tel,content) values(?,?,?,?,?,?) ";
        conn=DBUtil.getConnection();
        try {
            ptmt = conn.prepareStatement(sql);

            ptmt.setString(1, nickname);
            ptmt.setString(2, sex);
            ptmt.setString(3, qq);
            ptmt.setString(4, email);
            ptmt.setString(5, tel);
            ptmt.setString(6, content);
            //对于insert的sql,不需要使用rs
            ptmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    }

异常信息:
404错误

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • baidu_33403616 再写三行 4年前

    404错误的原因可能是你jsp页面路径不对
    最好在在jsp页面最前面前面加上
    <%String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";%>
    然后在head里面加上

    点赞 评论 复制链接分享
  • baidu_33403616 再写三行 4年前

    404错误的原因可能是你jsp页面路径不对
    最好在在jsp页面最前面前面加上
    <%String path = request.getContextPath();
    String basePath = request.getScheme() + "://"

    • request.getServerName() + ":" + request.getServerPort()
    • path + "/";%> 然后在head里面加上
    点赞 评论 复制链接分享