JavaWeb开发--超详细个人手写笔记代码步骤

1.准备工作

前面的学生javase开发,而javaweb开发需要服务器和网页。

具备: java mysql jdbc html+css+js

web服务器: tomcat服务器. 部署项目。

Apache Tomcat® - Apache Tomcat 8 Software Downloads

解压上面的软件即可 (不要放在中文目录和特殊符号的目录

启动tomcat服务器

如果你的启动时一闪而过,证明jdk配置有误。

没有配置JAVA_HOME=

访问tomcat

http://服务所在的ip:8080

1.1idea创建web工程

web工程表示里面既可以写java代码也可以放置页面资源。

1.2web工程部署到本地的tomcat服务器中

localhost本地服务器的地址

8080:本地服务器的端口号

qy174_web03表示项目部署的上下文名称

main.html:本地项目的资源名称

1.3设置默认首页

当项目启动时默认打开的资源名

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--设置默认首页-->
    <welcome-file-list>
        <welcome-file>main.html</welcome-file>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

2.动态网页

动态网页就是结合和java编程和数据库技术。而且动态网页中可以插入java代码。常用的动态网页

jsp,freemark,thymeleaf等这些都是常用的动态网页技术。 而我们这里主要讲解jsp.

2.1什么是jsp

jsp【java server page】java服务器网页。该网页经过服务器tomcat编译产生java代码.而且里面可以插入java代码。

2.2jsp中如何插入java代码

<%

java语法的代码

%>

2.3java内容输出到网页中

有两种方式:

第一种使用out对象输出

第二种:<%=表达式%>

<%
int a = 10;
int b = 20;
int c = a + b;
out.print("c="+c+"<br>");//第一种
out.print("a="+a+"<br>");
%>

c=<%=c%><br>//第二种
a=<%=a%>

<table border="1" cellpadding="0" cellspacing="0" width="800">
    <%
        for (int i = 1; i < 10; i++) {
            out.print("<tr>");
            for (int j = 1; j <= i; j++) {
                out.print("<td>"+j+"*"+i+"="+i*j+"</td>");
            }
            out.print("</tr>");
        }
    %>
   </table>
<table border="1" cellpadding="0" cellspacing="0" width="800">
       <%for (int i = 1; i < 10; i++) {%>
       <tr>
           <% for (int j = 1; j <= i; j++) {%>
           <td><%=j%> * <%=i%> = <%=j*i%></td>
           <%}%>
           <br>
       </tr>
        <%}%>
   </table>

错误类型

400~499:前端问题。 ==404路径找不到问题== 400: 参数类型不匹配[框架]。 405请求方式不匹配

500: 服务器错误【java功能错误---junit测试---->[发到群里---发出解决方案]】。

3.接受参数

表单提交和超链接传递参数时,我们需要接受传递过来的参数内容。并完成相应的业务功能。servlet中封装了HttpServletRequest类,该类可以操作所有的请求内容。而在jsp中内置了该类的对象request。[内置表示无需自己创建该类对象。就可以使用该类中的方法]

3.1如果idea无法在jsp中使用内置对象

在web-inf下创建一个目录lib,放入jsp和servlet的jar包

3.2解决中文乱码

当表单输入的内容为中文时,接受时出现了乱码文件。

调用request中setCharacterEncoding("utf-8")

设置请求的编码:注意接收参数前设置utf-8要和网页的编码一致。

编码种类:ISO-8859-1:英文编码 不支持中文

gbk:中文编码 繁体中文和简体中文

gb2312:中文编码,只支持简体中文

utf-8:万国码,经常使用

3.3接受表单的参数

<%--action:表单提交的路径  method:表单的提交方式--%>
<form action="registerDo.jsp" method="post">
        账号:<input name="zh" type="text"><br>
        密码:<input name="pwd" type="text"><br>
        性别:<input name="sex" type="radio" value="男">男
        <input name="sex" type="radio" value="女">女
        <br>
        爱好:<input name="hobby" type="checkbox" value="游泳">游泳
        <input name="hobby" type="checkbox" value="唱歌">唱歌
        <input name="hobby" type="checkbox" value="跳舞">跳舞
        <br>
        国家:<select name="country">
                    <option>china</option>
                    <option>USA</option>
                    <option>JAPANESE</option>
            </select><br>
        描述:<textarea name="desc"></textarea><br>
        <input type="submit" value="提交">
</form>

<body>
    <%
    //设置请求的编码:utf-8万国码
        request.setCharacterEncoding("utf-8");
    //内置HttpServletRequest的对象
    //接受请求的参数值
        String zh = request.getParameter("zh");
        String pwd = request.getParameter("pwd");
        String sex = request.getParameter("sex");
    //多选框--用getParameter只能拿到一个值。
        String[] hobby = request.getParameterValues("hobby");
        String country = request.getParameter("country");
        String desc = request.getParameter("desc");
    %>
    账号:<%=zh%><br>
    密码:<%=pwd%><br>
    性别:<%=sex%><br>
    爱好:<%=Arrays.toString(hobby)%><br>
    国家:<%=country%><br>
    描述:<%=desc%><br>
</body>

3.4超链接传递参数

地址?key=value&key=value

4.jsp+dao

<%--
  Created by IntelliJ IDEA.
  User: WJY
  Date: 2024/5/6
  Time: 15:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>验证首页</title>
</head>
<body>
<%String error = request.getParameter("error");
    if ("1".equals(error)){%>
    <script>
        alert("账号或密码错误");
    </script>
    <%}%>

<%String success = request.getParameter("success");
    if ("1".equals(success)){%>
    <script>
        alert("注册成功请登录!")
    </script>
    <%}%>

  <form action="login_test.jsp" method="post">
     账号:<input type="text" name="name" required/><br>
     密码:<input type="password" name="password" required/><br>
      <input type="submit" value="登录"/>
      <input type="button" value="注册" onclick="location.href='enroll.jsp'"/>
  </form>
</body>
</html>
<%@ page import="com.wjy.dao.UsersDao" %>
<%@ page import="com.wjy.entity.Users" %><%--
  Created by IntelliJ IDEA.
  User: WJY
  Date: 2024/5/6
  Time: 16:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title&g
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值