将值从JSP页面传递到Servlet页面

时间:2013-03-19 10:54:45

标签: jsp servlets

这是我的JSP页面我在接受一些值,我想将值发送到Servlet,然后存储在DB中。任何人可以帮助,这是正确的方法吗?还有什么应该在servlet中添加以检索那里的值..

<html>
    <head>
        <link rel="stylesheet" href="MainCSS.css" type="text/css">
        <title></title>
    </head>
    <body background-color:blue>
        <form action="HelloServlet_new.java" method="POST">
            <div style="width:600px; margin:0 auto;">
                <span style="color:#ffffff;"><span style="font-size: 18px;"><span style="font-family: 'times new roman', times, serif;"><strong><span style="background-color: rgb(0, 0, 255);"><a href="http://My/index_new"></a> cISBN | cAttendance | cFleet |&nbsp;cHome |</span></strong></span></span></span></div>
            <div>
                <span style="font-size: 18px;"><span style="font-family: 'times new roman', times, serif;"><strong>&nbsp;</strong></span></span></div>
            <div style="width:600px; margin:0 auto;">
                Enter Your Message Here: <input type="text" name="message" value="" /><br>
                Enter the Receiver Number: <input type="text" name="receiver" value="" />
                Enter Your Name: <input type="text" name="name" value="" /><br><br>
                Enter Your Number: <input type="text" name="sender" value="" /><br><br>
                Enter Your Mail_id: <input type="text" name="mailid" value="" />
                <input type="submit" name="submit" value="Send" /><br>

            </div>

           <% request.setAttribute("message",request.getParameter("message"));%>
           <% request.setAttribute("receiver",request.getParameter("receiver"));%>
           <% request.setAttribute("name",request.getParameter("name"));%>
            <%request.setAttribute("sender",request.getParameter("sender"));%>
           <% request.setAttribute("mailid",request.getParameter("mailid"));%>

            <h2>Thank You for using Our Service !!!!!!! </h2>
            <jsp:include page="HelloServlet_new" flush="true" />
    </body>
</form>
</html>

1 个答案:

答案 0 :(得分:3)

首先在web.xml中映射您的servlet。 在表单的操作字段内,记下与web.xml中提供的servlet的<url-pattern>匹配的URL。例如,如果是/urlPattern,那么您应该写下来:

<form action="urlPattern" method="POST">

在您调用的servlet doPost()中,您可以通过编写:

来检索JSP页面值
String message = request.getParameter("message");
String receiver = request.getParameter("receiver");

这是您可以在servlet中获取值的方法,之后您可以记下数据库事务代码以将值存储在数据库中。