yiqianyan 2017-03-28 04:46 采纳率: 100%
浏览 1244
已采纳

jsp+servlet+jdbc+web.xml

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条回答 默认 最新

  • 再写三行 2017-03-28 05:01
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 文本数据挖掘分析报告
  • ¥15 代写uni代码,app唤醒
  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部