WEBAPP开发之servlet开发简单Java Web项目
白羽 2019-01-08 来源 :网络 阅读 911 评论 0

摘要:本文将带你了解WEBAPP开发servlet开发简单Java Web项目,希望本文对大家学WEBAPP有所帮助。

    本文将带你了解WEBAPP开发servlet开发简单Java Web项目,希望本文对大家学WEBAPP有所帮助。


               

                 

1、表单

这里form中action不能带"/",不带/表示相对路径,带“/”表示绝对路径,必须写成/项目名称/url。


<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "//www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>用户登录</title></head><body>    <div>     <form action="loginServlet" method="post">         <table>             <tr>                 <td>用户名:</td>                 <td><input id="userCode" name="userCode" type="text"></td>             </tr>             <tr>                 <td>密码:</td>                 <td><input type="password" name="password"></td>             </tr>             <tr>                 <td><input type="submit" value="登录"></td>             </tr>         </table>     </form>    </div></body></html>

2、web.xml配置

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns="//java.sun.com/xml/ns/javaee" xmlns:web="//java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="//java.sun.com/xml/ns/javaee //java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>pro-web02</display-name>  <welcome-file-list>    <welcome-file>login.jsp</welcome-file>    <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>loginServlet</servlet-name>    <servlet-class>cn.com.login.web.LoginController</servlet-class></servlet><servlet-mapping>    <servlet-name>loginServlet</servlet-name>    <url-pattern>/loginServlet</url-pattern></servlet-mapping></web-app>

这里的url-pattern中必须带/,不然会报错java.lang.IllegalArgumentException:   Invalid <url-pattern> in servlet   mapping


3、LoginController重写doPost和doGet代码

参考w3cSchool中servlet教程 

//www.runoob.com/servlet/servlet-form-data.html  

public class LoginController extends HttpServlet{ private static final long serialVersionUID = 1L; public LoginController() { super(); } public void doGet(HttpServletRequest request,            HttpServletResponse response)    throws ServletException, IOException { // 设置响应内容类型 response.setContentType("text/html;charset=UTF-8"); System.out.println("用户名" + request.getParameter("userCode")); PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n"; out.println(docType +         "<html>\n" +         "<head><title>" + title + "</title></head>\n" +         "<body bgcolor=\"#f0f0f0\">\n" +         "<h1 align=\"center\">" + title + "</h1>\n" +         "<ul>\n" +         "  <li><b>名字</b>:"         + request.getParameter("userCode") + "\n" +         "</ul>\n" +         "</body></html>"); }// 处理 POST 方法请求的方法public void doPost(HttpServletRequest request,             HttpServletResponse response) throws ServletException, IOException { doGet(request, response);}}


                     

                 

                 

                

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之WebApp频道!

本文由 @白羽 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程