这篇文章主要介绍如何使用JavaWeb实现表单提交,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
register.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>register.html</title></head><body><center><h2>注册表单</h2></center><center><form action="register" method="get">用户名:<input type="text" name="username"/><br/>密码:<input type="password" name="pwd"/><br/> 请选择感兴趣的研究方向: <label for="01">人工智能</label> <input type="checkbox" name="interest" value="AI" id="01" /> <label for="02">机器学习</label> <input type="checkbox" name="interest" value="ML" id="02" /> <label for="03">自动驾驶</label> <input type="checkbox" name="interest" value="AutoDrive" id="03" /> <label for="04">网络安全</label> <input type="checkbox" name="interest" value="Network" id="04" /> <br/> <input type="submit" name="smt" value="提交"/></form></center></body></html>
RegisterServlet.java
package request;import java.io.IOException;import java.io.PrintWriter;import java.util.Arrays;import java.util.Enumeration;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class RegisterServlet extends HttpServlet{//调出doGet方法protected void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException {//使用req接收请求参数String strName=req.getParameter("username");//处理中文乱发问题byte[] bs=strName.getBytes("iso8859-1");String name=new String(bs,"utf-8");System.out.println(name);}//调出doPost方法protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException {//设置请求参数中的编码req.setCharacterEncoding("utf-8");//使用req接收请求参数String strName=req.getParameter("username");System.out.println("strName");}//调出service方法protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{PrintWriter pw=response.getWriter();System.out.println("注册。。。");//1.获取浏览器端发送请求的完整路径;StringBuffer sbuf=request.getRequestURL();pw.println("url:"+sbuf);//System.out.println(sbuf);//2.获取资源名部分-请求路径中8080后的地址;String uri=request.getRequestURI();pw.println("uri:"+uri); //3.获取请求路径中参数;String qs=request.getQueryString();pw.println("qs:"+qs);//4.获取请求路径中的ip地址;String ip=request.getRemoteAddr();pw.println("ip:"+ip);//ip=127.0.0.1//5.获取浏览器发送请求的方式(get请求、post请求)//在浏览器端,浏览器地址栏回车、超链接、表单的默认方式都是get请求方式//在浏览器端,表单设置method="post"的方式提交是post请求方式String method=request.getMethod();pw.println("method="+method);//method=GET//6.获取web应用名String cp=request.getContextPath();pw.println("cp="+cp);//cp=/servlet//接收表单提交发送过来的参数//获得单个value值String strName=request.getParameter("username");String strPwd=request.getParameter("pwd");pw.println("Name:"+strName+" Pwd:"+strPwd);//获得多个对应value值String[] strInterest=request.getParameterValues("interest");pw.println(Arrays.toString(strInterest));//获取请求参数中所有的name值Enumeration<String> names=request.getAttributeNames();pw.println(names);//使用循环将names中的内容取出while(names.hasMoreElements()){System.out.println(names.nextElement());}}}
修改web.xml,添加如下code
<!-- 配置RegisterServlet --> <servlet> <servlet-name>RegisterServlet</servlet-name> <!-- 配置Servlet的路径:包名+类名 --> <servlet-class>request.RegisterServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterServlet</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping>
重新配置服务器
先点击左侧图标
再点击Redeploy,重新部署Tomcat服务器
以上是“如何使用JavaWeb实现表单提交”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!