html性别选择默认值怎么设置为session中的值?

html性别选择默认值怎么设置为session中的值?,第1张

<input name="sex" type="radio" value="1" checked />男

<input name="sex" type="radio" value="2" />女

获取更新之前的值:$("input[name='sex']:checked").val()

表单:

<form action="doservlet" method="post">

姓名:<input name="user_name"><br>

性别<input type="radio" value="man" name="sex">男 <input type="radio" value="women" name="sex">女<Br />

爱好:<input type="check" name="likes" value="运动" />运动

<input type="check" name="likes" value="读书" />读书

<input type="check" name="likes" value="音乐" />音乐

<input type="check" name="likes" value="书法" />书法

</form>

servlet:

iimport java.io.IOException

import java.io.PrintWriter

import javax.servlet.ServletException

import javax.servlet.http.HttpServlet

import javax.servlet.http.HttpServletRequest

import javax.servlet.http.HttpServletResponse

public class doservlet extends HttpServlet {

/**

* Constructor of the object.

*/

public doservlet() {

super()

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy()// Just puts "destroy" string in log

// Put your code here

}

/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html")

PrintWriter out = response.getWriter()

out

.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">")

out.println("<HTML>")

out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>")

out.println(" <BODY>")

out.print("This is ")

out.print(this.getClass())

out.println(", using the GET method")

out.println(" </BODY>")

out.println("</HTML>")

out.flush()

out.close()

}

/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html")

PrintWriter out = response.getWriter()

String name = null

String sex = null

String likes[] = null

name=request.getParameter("user_name")

sex=request.getParameter("sex")

likes=request.getParameterValues("likes")

request.getSession().setAttribute("user_name", name)

request.getSession().setAttribute("sex", sex)

request.getSession().setAttribute("like",likes)

//把请求过来的数据放在session里

response.sendRedirect("目的页")

//在目的页中通过session的 getAttribute方法取出来即可

out.flush()

out.close()

}

/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occure

*/

public void init() throws ServletException {

// Put your code here

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/7173079.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-02
下一篇2023-04-02

发表评论

登录后才能评论

评论列表(0条)

    保存