Using JSPs

Using JSPs

While we could output the HTML for our user interface directly from the Java servlet code, this would be difficult to maintain as the HTML gets complicated. It's better to use a template system, with the user interface designed and implemented in separate files with placeholders and logic to insert data provided by the application. There are many template systems available for Java, any of which would work with App Engine.

大意:我们可以在servlet中,输出html代码,但这使得这些html代码难于维护。它最好使用模版系统,这些模版有些设计了用户界面,实现了通过包含占位符的单独文件和逻辑插入数据的应用。有许多java可用的模版系统,任何个模版都可应用于App Engine

For this tutorial, we'll use JavaServer Pages (JSPs) to implement the user interface for the guest book. JSPs are part of the servlet standard. App Engine compiles JSP files in the application's WAR automatically, and maps them to URL paths.

大意:在这个向导中,我们将使用JSPs 来实现guestbook的用户界面,jspsservlet标准的一部分,App Engin自动编译WAR目录下的JSP文件,并且映射到URL路径上。

Hello, JSP!

Our guest book app writes strings to an output stream, but this could also be written as a JSP. Let's begin by porting the latest version of the example to a JSP.

In the directory war/, create a file named guestbook.jsp with the following contents:

大意:我们的guset book应用通过一个输出流将字符串输出,这些同样能够以jsp方式来写,我们开始把最后一个版本的guest book应用移植到jsp上。在目录war/下,创建一个名为guestbook.jsp,其有如下内容:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.google.appengine.api.users.User" %>

<%@ page import="com.google.appengine.api.users.UserService" %>

<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<html>

  <body>

<%

    UserService userService = UserServiceFactory.getUserService();

    User user = userService.getCurrentUser();

    if (user != null) {

%>

Hello, <%= user.getNickname() %>! (You can

<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out.)

<%

    } else {

%>

Hello!

<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in

to include your name with greetings you post.

<%

    }

%>

  </body>

</html>

By default, any file in war/ or a subdirectory (other than WEB-INF/) whose name ends in .jsp is automatically mapped to a URL path. The URL path is the path to the .jsp file, including the filename. This JSP will be mapped automatically to the URL /guestbook.jsp.

大意:默认情况下,任何在war目录下或其子目录下并以.jsp结尾的文件都会被自动映射到一个URL路径。这个URL路径是这个.jsp文件的路径,包括文件名。此jsp文件将会自动映射到 /guestbook.jsp

For the guest book app, we want this to be the application's home page, displayed when someone accesses the URL /. An easy way to do this is to declare in web.xml that guestbook.jsp is the "welcome" servlet for that path.

大意:对于这个guest book应用,我想使用这个页面作为整个应用的主页面,即当用户输入/时,显示的就是这个页面。一个简单的方式就是在web.xml中通过“welcome”标签来配置guestbook.jsp

Edit war/WEB-INF/web.xml and replace the current <welcome-file> element in the <welcome-file-list>. Be sure to remove index.html from the list, as static files take precedence over JSP and servlets.

大意:编辑war/WEB-INF/web.xml,替换在<welcome-file-list>中的<welcome-file>的配置,将index.htm移除,换上你想使用的jsp,如下:

    <welcome-file-list>

        <welcome-file>guestbook.jsp</welcome-file>

    </welcome-file-list>

Tip: If you are using Eclipse, the editor may open this file in "Design" mode. To edit this file as XML, select the "Source" tab at the bottom of the frame.

Stop then start the development server. Visit the following URL:

提示:如果你使用Eclipse,编辑器可能以“设计(Design)”模式打开。为了编辑此XML代码,请选择在版面底部的”source”

停止然后启动服务,使用如下URL访问:

The app displays the contents of guestbook.jsp, including the user nickname if the user is signed in.

大意:这个应用显示了guestbook.jsp的内容,如果用户登录了,将显示其昵称

When you load a JSP for the first time, the development server converts the JSP into Java source code, then compiles the Java source into Java bytecode. The Java source and the compiled class are saved to a temporary directory. The development server regenerates and compiles JSPs automatically if the original JSP files change.

大意:当你第一次载入JSP,应用服务器将会将jsp转化为java源代码,然后将这些java源代码转化为java字节码,这些源代码和这些编译好的类被存放在一个临时的目录里。如果这些jsp文件改变了,应用服务器将更新和编译jsp文件。

When you upload your application to App Engine, the SDK compiles all JSPs to bytecode, and only uploads the bytecode. When your app is running on App Engine, it uses the compiled JSP classes.

大意:当你上传你的应用到App Engine时,SDK把所有的jsp文件编译成字节码文件,并只上传这些字节码文件。当你的应用跑在App Engine上时,它使用的是这些编译好的JSP类。

The Guestbook Form

Our guest book application will need a web form so the user can post a new greeting, and a way to process that form. The HTML of the form will go into the JSP. The destination of the form will be a new URL, /sign, to be handled by a new servlet class, SignGuestbookServlet. SignGuestbookServlet will process the form, then redirect the user's browser back to /guestbook.jsp. For now, the new servlet will just write the posted message to the log.

Edit guestbook.jsp, and put the following lines just above the closing </body> tag:

大意:guestbook应用将需要一个表单,使得用户能够提交一个新的问候,并且需要一个处理表单的方法。在JSP中可以使用HTML表单,这个表单将被提交到/sign,一个新的处理servlet类,SignGuestbookServlet,这个类将处理这个表单,然后转到/guestbook.jsp页面,目前,这个新的servlet仅仅只是把请求的消息写到日志中。编辑guestbook.jsp,把下面的几行放到<body></body>标签中。

  ...

 

  <form action="/sign" method="post">

    <div><textarea name="content" rows="3" cols="60"></textarea></div>

    <div><input type="submit" value="Post Greeting" /></div>

  </form>

  </body>

</html>

Create a new class named SignGuestbookServlet in the package guestbook. (Non-Eclipse users, create the file SignGuestbookServlet.java in the directory src/guestbook/.) Give the source file the following contents:

大意:在src/guestbook/目录下创建一个新的Servlet:SignGuestbookServlet:

package guestbook;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class SignGuestbookServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String content = req.getParameter("content");
        if (content == null) {
            content = "(No greeting)";
        }
        if (user != null) {
            log.info("Greeting posted by user " + user.getNickname() + ": " + content);
        } else {
            log.info("Greeting posted anonymously: " + content);
        }
        resp.sendRedirect("/guestbook.jsp");
    }
}

Edit war/WEB-INF/web.xml and add the following lines to declare the SignGuestbookServlet servlet and map it to the the /sign URL:

大意:修改war/WEB-INF/web.xml ,添加如下行声明SignGuestbookServlet的映射路径为 /sign

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

    ...

 

    <servlet>

        <servlet-name>sign</servlet-name>

        <servlet-class>guestbook.SignGuestbookServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>sign</servlet-name>

        <url-pattern>/sign</url-pattern>

    </servlet-mapping>

 

    ...

</web-app>

This new servlet uses the java.util.logging.Logger class to write messages to the log. You can control the behavior of this class using a logging.properties file, and a system property set in the app's appengine-web.xml file. If you are using Eclipse, your app was created with a default version of this file in your app's src/ and the appropriate system property.

大意:这个sevlet使用了java.util.loggin.Logger类将信息写出到日志。通过使用logging.properties文件改变其行为,如果你正使用Eclipse,你的应用将在src目录下创建一个默认的文件版本及适当的系统属性。

If you are not using Eclipse, you must set up the Logger configuration file manually. Copy the example file from the SDK appengine-java-sdk/config/user/logging.properties to your app's war/WEB-INF/ directory. Then edit the app's war/WEB-INF/appengine-web.xml file as indicated:

大意:如果你没使用Eclipse,你必须手动配置logger配置,将appengine-java-sdk/config/user/logging.properties复制至你的应用war/WEB-INF/directory下。然后编辑这个应用下的:

War/WEB-INF/appengine-web.xml文件如下:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    ...

    <system-properties>

        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>

    </system-properties>

</appengine-web-app>

The servlet logs messages using the INFO log level (using log.info()). The default log level is WARNING, which suppresses INFO messages from the output. To change the log level for all classes in the guestbook package, edit the logging.properties file and add an entry for guestbook.level, as follows:

Servlet 日志消息使用INFO日志标准(使用log.info())。默认的日志标准为Warning,如果要改变在guestbook包下,所有类文件的日志标准,需要修改logging.properties文件,并添加一个条目,如下:

.level = WARNING

guestbook.level = INFO

...

Tip: When your app logs messages using the java.util.logging.Logger API while running on App Engine, App Engine records the messages and makes them available for browsing in the Admin Console, and available for downloading using the AppCfg tool. The Admin Console lets you browse messages by log level.

Rebuild and restart, then test http://localhost:8080/. The form displays. Enter some text in the form, and submit. The browser sends the form to the app, then redirects back to the empty form. The greeting data you entered is logged to the console by the server.

提示:当你的应用日志使用java.util.logging.Logger API,当运行在App Engine时,App Engine记录这个消息,并且使他们显示在管理员控治台上,并且可使用AppCfg工具下载。管理员控制台可以通过日志标准来显示消息。

重新部署,然后重启,通过http://localhost:8080/,显示表单。输入一些文本,提交,浏览器将提交表单,然后返回一个空表单。你输入的数据被服务器记录到了控制台。

Using JSPs

While we could output the HTML for our user interface directly from the Java servlet code, this would be difficult to maintain as the HTML gets complicated. It's better to use a template system, with the user interface designed and implemented in separate files with placeholders and logic to insert data provided by the application. There are many template systems available for Java, any of which would work with App Engine.

大意:我们可以在servlet中,输出html代码,但这使得这些html代码难于维护。它最好使用模版系统,这些模版有些设计了用户界面,实现了通过包含占位符的单独文件和逻辑插入数据的应用。有许多java可用的模版系统,任何个模版都可应用于App Engine

For this tutorial, we'll use JavaServer Pages (JSPs) to implement the user interface for the guest book. JSPs are part of the servlet standard. App Engine compiles JSP files in the application's WAR automatically, and maps them to URL paths.

大意:在这个向导中,我们将使用JSPs 来实现guestbook的用户界面,jspsservlet标准的一部分,App Engin自动编译WAR目录下的JSP文件,并且映射到URL路径上。

Hello, JSP!

Our guest book app writes strings to an output stream, but this could also be written as a JSP. Let's begin by porting the latest version of the example to a JSP.

In the directory war/, create a file named guestbook.jsp with the following contents:

大意:我们的guset book应用通过一个输出流将字符串输出,这些同样能够以jsp方式来写,我们开始把最后一个版本的guest book应用移植到jsp上。在目录war/下,创建一个名为guestbook.jsp,其有如下内容:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ page import="com.google.appengine.api.users.User" %>

<%@ page import="com.google.appengine.api.users.UserService" %>

<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

 

<html>

  <body> 

<%

    UserService userService = UserServiceFactory.getUserService();

    User user = userService.getCurrentUser();

    if (user != null) {

%>

Hello, <%= user.getNickname() %>! (You can

<a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out.)

 

<%

    } else {

%>

Hello!

<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in

to include your name with greetings you post.

 

<%

    }

%>

 

  </body>

</html>

By default, any file in war/ or a subdirectory (other than WEB-INF/) whose name ends in .jsp is automatically mapped to a URL path. The URL path is the path to the .jsp file, including the filename. This JSP will be mapped automatically to the URL /guestbook.jsp.

大意:默认情况下,任何在war目录下或其子目录下并以.jsp结尾的文件都会被自动映射到一个URL路径。这个URL路径是这个.jsp文件的路径,包括文件名。此jsp文件将会自动映射到 /guestbook.jsp

For the guest book app, we want this to be the application's home page, displayed when someone accesses the URL /. An easy way to do this is to declare in web.xml that guestbook.jsp is the "welcome" servlet for that path.

大意:对于这个guest book应用,我想使用这个页面作为整个应用的主页面,即当用户输入/时,显示的就是这个页面。一个简单的方式就是在web.xml中通过“welcome”标签来配置guestbook.jsp

Edit war/WEB-INF/web.xml and replace the current <welcome-file> element in the <welcome-file-list>. Be sure to remove index.html from the list, as static files take precedence over JSP and servlets.

大意:编辑war/WEB-INF/web.xml,替换在<welcome-file-list>中的<welcome-file>的配置,将index.htm移除,换上你想使用的jsp,如下:

    <welcome-file-list>

        <welcome-file>guestbook.jsp</welcome-file>

    </welcome-file-list>

Tip: If you are using Eclipse, the editor may open this file in "Design" mode. To edit this file as XML, select the "Source" tab at the bottom of the frame.

Stop then start the development server. Visit the following URL:

提示:如果你使用Eclipse,编辑器可能以“设计(Design)”模式打开。为了编辑此XML代码,请选择在版面底部的”source”

停止然后启动服务,使用如下URL访问:

The app displays the contents of guestbook.jsp, including the user nickname if the user is signed in.

大意:这个应用显示了guestbook.jsp的内容,如果用户登录了,将显示其昵称

When you load a JSP for the first time, the development server converts the JSP into Java source code, then compiles the Java source into Java bytecode. The Java source and the compiled class are saved to a temporary directory. The development server regenerates and compiles JSPs automatically if the original JSP files change.

大意:当你第一次载入JSP,应用服务器将会将jsp转化为java源代码,然后将这些java源代码转化为java字节码,这些源代码和这些编译好的类被存放在一个临时的目录里。如果这些jsp文件改变了,应用服务器将更新和编译jsp文件。

When you upload your application to App Engine, the SDK compiles all JSPs to bytecode, and only uploads the bytecode. When your app is running on App Engine, it uses the compiled JSP classes.

大意:当你上传你的应用到App Engine时,SDK把所有的jsp文件编译成字节码文件,并只上传这些字节码文件。当你的应用跑在App Engine上时,它使用的是这些编译好的JSP类。

The Guestbook Form

Our guest book application will need a web form so the user can post a new greeting, and a way to process that form. The HTML of the form will go into the JSP. The destination of the form will be a new URL, /sign, to be handled by a new servlet class, SignGuestbookServlet. SignGuestbookServlet will process the form, then redirect the user's browser back to /guestbook.jsp. For now, the new servlet will just write the posted message to the log.

Edit guestbook.jsp, and put the following lines just above the closing </body> tag:

大意:guestbook应用将需要一个表单,使得用户能够提交一个新的问候,并且需要一个处理表单的方法。在JSP中可以使用HTML表单,这个表单将被提交到/sign,一个新的处理servlet类,SignGuestbookServlet,这个类将处理这个表单,然后转到/guestbook.jsp页面,目前,这个新的servlet仅仅只是把请求的消息写到日志中。编辑guestbook.jsp,把下面的几行放到<body></body>标签中。

  ...

 

  <form action="/sign" method="post">

    <div><textarea name="content" rows="3" cols="60"></textarea></div>

    <div><input type="submit" value="Post Greeting" /></div>

  </form>

  </body>

</html>

Create a new class named SignGuestbookServlet in the package guestbook. (Non-Eclipse users, create the file SignGuestbookServlet.java in the directory src/guestbook/.) Give the source file the following contents:

大意:在src/guestbook/目录下创建一个新的Servlet:SignGuestbookServlet:

package guestbook;

import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.http.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class SignGuestbookServlet extends HttpServlet {
    private static final Logger log = Logger.getLogger(SignGuestbookServlet.class.getName());

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        String content = req.getParameter("content");
        if (content == null) {
            content = "(No greeting)";
        }
        if (user != null) {
            log.info("Greeting posted by user " + user.getNickname() + ": " + content);
        } else {
            log.info("Greeting posted anonymously: " + content);
        }
        resp.sendRedirect("/guestbook.jsp");
    }
}

Edit war/WEB-INF/web.xml and add the following lines to declare the SignGuestbookServlet servlet and map it to the the /sign URL:

大意:修改war/WEB-INF/web.xml ,添加如下行声明SignGuestbookServlet的映射路径为 /sign

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">

    ...

 

    <servlet>

        <servlet-name>sign</servlet-name>

        <servlet-class>guestbook.SignGuestbookServlet</servlet-class>

    </servlet>

    <servlet-mapping>

        <servlet-name>sign</servlet-name>

        <url-pattern>/sign</url-pattern>

    </servlet-mapping>

 

    ...

</web-app>

This new servlet uses the java.util.logging.Logger class to write messages to the log. You can control the behavior of this class using a logging.properties file, and a system property set in the app's appengine-web.xml file. If you are using Eclipse, your app was created with a default version of this file in your app's src/ and the appropriate system property.

大意:这个sevlet使用了java.util.loggin.Logger类将信息写出到日志。通过使用logging.properties文件改变其行为,如果你正使用Eclipse,你的应用将在src目录下创建一个默认的文件版本及适当的系统属性。

If you are not using Eclipse, you must set up the Logger configuration file manually. Copy the example file from the SDK appengine-java-sdk/config/user/logging.properties to your app's war/WEB-INF/ directory. Then edit the app's war/WEB-INF/appengine-web.xml file as indicated:

大意:如果你没使用Eclipse,你必须手动配置logger配置,将appengine-java-sdk/config/user/logging.properties复制至你的应用war/WEB-INF/directory下。然后编辑这个应用下的:

War/WEB-INF/appengine-web.xml文件如下:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">

    ...

 

    <system-properties>

        <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>

    </system-properties>

 

</appengine-web-app>

The servlet logs messages using the INFO log level (using log.info()). The default log level is WARNING, which suppresses INFO messages from the output. To change the log level for all classes in the guestbook package, edit the logging.properties file and add an entry for guestbook.level, as follows:

Servlet 日志消息使用INFO日志标准(使用log.info())。默认的日志标准为Warning,如果要改变在guestbook包下,所有类文件的日志标准,需要修改logging.properties文件,并添加一个条目,如下:

.level = WARNING

guestbook.level = INFO 

...

Tip: When your app logs messages using the java.util.logging.Logger API while running on App Engine, App Engine records the messages and makes them available for browsing in the Admin Console, and available for downloading using the AppCfg tool. The Admin Console lets you browse messages by log level.

Rebuild and restart, then test http://localhost:8080/. The form displays. Enter some text in the form, and submit. The browser sends the form to the app, then redirects back to the empty form. The greeting data you entered is logged to the console by the server.

提示:当你的应用日志使用java.util.logging.Logger API,当运行在App Engine时,App Engine记录这个消息,并且使他们显示在管理员控治台上,并且可使用AppCfg工具下载。管理员控制台可以通过日志标准来显示消息。

重新部署,然后重启,通过http://localhost:8080/,显示表单。输入一些文本,提交,浏览器将提交表单,然后返回一个空表单。你输入的数据被服务器记录到了控制台。

 

ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值